forked from swiftlang/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Proof of concept for JITing functions in a pass.
- Loading branch information
Showing
8 changed files
with
124 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
//===-------------------------- MyJITPass.h ---------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_TRANSFORMS_UTILS_MYJITPASS_H | ||
#define LLVM_TRANSFORMS_UTILS_MYJITPASS_H | ||
|
||
#include "llvm/IR/PassManager.h" | ||
|
||
namespace llvm { | ||
class MyJITPass : public PassInfoMixin<MyJITPass> { | ||
public: | ||
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); | ||
}; | ||
|
||
} // namespace llvm | ||
|
||
#endif // LLVM_TRANSFORMS_UTILS_MYJITPASS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
//===---------------------------- MyJITPass.cpp ---------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "llvm/Transforms/Utils/MyJITPass.h" | ||
#include "llvm/ExecutionEngine/Orc/LLJIT.h" | ||
|
||
using namespace llvm; | ||
using namespace llvm::orc; | ||
|
||
PreservedAnalyses MyJITPass::run(Function &F, | ||
FunctionAnalysisManager &AM) { | ||
auto J = LLJITBuilder().create(); | ||
if (!J) { | ||
errs() << "MyJITPass could not create JIT instance for " | ||
<< F.getName() << ": " << toString(J.takeError()) << "\n"; | ||
return PreservedAnalyses::all(); | ||
} | ||
|
||
auto I32I32FnTy = FunctionType::get(Type::getInt32Ty(F.getContext()), | ||
{Type::getInt32Ty(F.getContext())}, false); | ||
if (F.getFunctionType() != I32I32FnTy) { | ||
errs() << "MyJITPass: Function " << F.getName() | ||
<< " does not have required type. Skipping.\n"; | ||
return PreservedAnalyses::all(); | ||
} | ||
|
||
auto FM = cloneToNewContext(*F.getParent(), | ||
[&](const GlobalValue &GV) { return &GV == &F; }); | ||
if (auto Err = (*J)->addIRModule(std::move(FM))) { | ||
errs() << "MyJITPass could not add extracted module for " | ||
<< F.getName() << ": " << toString(std::move(Err)) << "\n"; | ||
return PreservedAnalyses::all(); | ||
} | ||
|
||
auto FSym = (*J)->lookup(F.getName()); | ||
if (!FSym) { | ||
errs() << "MyJITPass could not get JIT'd symbol for " | ||
<< F.getName() << ": " << toString(FSym.takeError()) << "\n"; | ||
return PreservedAnalyses::all(); | ||
} | ||
|
||
auto *JittedF = FSym->toPtr<int32_t(int32_t)>(); | ||
errs() << "Result: " << JittedF(42) << "\n"; | ||
|
||
return PreservedAnalyses::all(); | ||
} |