Proteus
Programmable JIT compilation and optimization for C/C++ using LLVM
Loading...
Searching...
No Matches
TransformArgumentSpecialization.hpp
Go to the documentation of this file.
1//===-- TransformArgumentSpecialization.hpp -- Specialize arguments --===//
2//
3// Part of the Proteus Project, under the Apache License v2.0 with LLVM
4// Exceptions. See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9//===----------------------------------------------------------------------===//
10
11#ifndef PROTEUS_TRANSFORM_ARGUMENT_SPECIALIZATION_HPP
12#define PROTEUS_TRANSFORM_ARGUMENT_SPECIALIZATION_HPP
13
14#include <llvm/IR/IRBuilder.h>
15#include <llvm/Support/Debug.h>
16
18#include "proteus/Utils.h"
19
20namespace proteus {
21
22using namespace llvm;
23
25public:
26 static void transform(Module &M, Function &F,
27 const SmallVectorImpl<int32_t> &ArgPos,
28 ArrayRef<RuntimeConstant> RC) {
29 auto &Ctx = M.getContext();
30
31 // Replace argument uses with runtime constants.
32 for (size_t I = 0; I < ArgPos.size(); ++I) {
33 int ArgNo = ArgPos[I];
34 Value *Arg = F.getArg(ArgNo);
35 Type *ArgType = Arg->getType();
36 Constant *C = nullptr;
37 if (ArgType->isIntegerTy(1)) {
38 C = ConstantInt::get(ArgType, RC[I].Value.BoolVal);
39 } else if (ArgType->isIntegerTy(8)) {
40 C = ConstantInt::get(ArgType, RC[I].Value.Int8Val);
41 } else if (ArgType->isIntegerTy(32)) {
42 // Logger::logs("proteus") << "RC is Int32\n";
43 C = ConstantInt::get(ArgType, RC[I].Value.Int32Val);
44 } else if (ArgType->isIntegerTy(64)) {
45 // Logger::logs("proteus") << "RC is Int64\n";
46 C = ConstantInt::get(ArgType, RC[I].Value.Int64Val);
47 } else if (ArgType->isFloatTy()) {
48 // Logger::logs("proteus") << "RC is Float\n";
49 C = ConstantFP::get(ArgType, RC[I].Value.FloatVal);
50 }
51 // NOTE: long double on device should correspond to plain double.
52 // XXX: CUDA with a long double SILENTLY fails to create a working
53 // kernel in AOT compilation, with or without JIT.
54 else if (ArgType->isDoubleTy()) {
55 // Logger::logs("proteus") << "RC is Double\n";
56 C = ConstantFP::get(ArgType, RC[I].Value.DoubleVal);
57 } else if (ArgType->isX86_FP80Ty() || ArgType->isPPC_FP128Ty() ||
58 ArgType->isFP128Ty()) {
59 C = ConstantFP::get(ArgType, RC[I].Value.LongDoubleVal);
60 } else if (ArgType->isPointerTy()) {
61 auto *IntC =
62 ConstantInt::get(Type::getInt64Ty(Ctx), RC[I].Value.Int64Val);
63 C = ConstantExpr::getIntToPtr(IntC, ArgType);
64 } else {
65 std::string TypeString;
66 raw_string_ostream TypeOstream(TypeString);
67 ArgType->print(TypeOstream);
68 PROTEUS_FATAL_ERROR("JIT Incompatible type in runtime constant: " +
69 TypeOstream.str());
70 }
71
72 PROTEUS_DBG(Logger::logs("proteus") << "[ArgSpecial] Replaced Function "
73 << F.getName() + " ArgNo " << ArgNo
74 << " with value " << *C << "\n");
75 Arg->replaceAllUsesWith(C);
76 }
77 }
78};
79
80} // namespace proteus
81
82#endif
#define PROTEUS_DBG(x)
Definition Debug.h:7
#define PROTEUS_FATAL_ERROR(x)
Definition Error.h:4
static llvm::raw_ostream & logs(const std::string &Name)
Definition Logger.hpp:18
Definition TransformArgumentSpecialization.hpp:24
static void transform(Module &M, Function &F, const SmallVectorImpl< int32_t > &ArgPos, ArrayRef< RuntimeConstant > RC)
Definition TransformArgumentSpecialization.hpp:26
Definition JitEngine.cpp:20