[llvm] [ExceptionDemo] Correct and update example ExceptionDemo (PR #69485)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Nov 14 01:15:23 PST 2023
https://github.com/epitavy updated https://github.com/llvm/llvm-project/pull/69485
>From 53f4ecb5f1fdd0c1c29d5ae8c4cdbded5766b947 Mon Sep 17 00:00:00 2001
From: Eliaz Pitavy <eliaz.pitavy at obspm.fr>
Date: Wed, 18 Oct 2023 19:05:26 +0200
Subject: [PATCH 1/4] [ExceptionDemo] Transition example from MCJIT to ORC and
fix compiling errors (#63702)
---
llvm/examples/ExceptionDemo/CMakeLists.txt | 4 +-
llvm/examples/ExceptionDemo/ExceptionDemo.cpp | 168 ++++++++----------
2 files changed, 80 insertions(+), 92 deletions(-)
diff --git a/llvm/examples/ExceptionDemo/CMakeLists.txt b/llvm/examples/ExceptionDemo/CMakeLists.txt
index 793cf291ca6f110..0a60ad848dd406d 100644
--- a/llvm/examples/ExceptionDemo/CMakeLists.txt
+++ b/llvm/examples/ExceptionDemo/CMakeLists.txt
@@ -1,9 +1,7 @@
set(LLVM_LINK_COMPONENTS
Core
ExecutionEngine
- MC
- MCJIT
- RuntimeDyld
+ ORCJIT
Support
Target
nativecodegen
diff --git a/llvm/examples/ExceptionDemo/ExceptionDemo.cpp b/llvm/examples/ExceptionDemo/ExceptionDemo.cpp
index 1b3ec7c91ddee10..97deb7b4393fbcb 100644
--- a/llvm/examples/ExceptionDemo/ExceptionDemo.cpp
+++ b/llvm/examples/ExceptionDemo/ExceptionDemo.cpp
@@ -49,8 +49,9 @@
#include "llvm/ADT/STLExtras.h"
#include "llvm/BinaryFormat/Dwarf.h"
-#include "llvm/ExecutionEngine/MCJIT.h"
-#include "llvm/ExecutionEngine/SectionMemoryManager.h"
+#include "llvm/ExecutionEngine/Orc/Core.h"
+#include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
+#include "llvm/ExecutionEngine/Orc/LLJIT.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/IRBuilder.h"
@@ -84,6 +85,8 @@
#define USE_GLOBAL_STR_CONSTS true
#endif
+llvm::ExitOnError ExitOnErr;
+
//
// Example types
//
@@ -142,6 +145,7 @@ static llvm::ConstantInt *ourExceptionCaughtState;
typedef std::vector<std::string> ArgNames;
typedef std::vector<llvm::Type*> ArgTypes;
+typedef llvm::ArrayRef<llvm::Type *> TypeArray;
//
// Code Generation Utilities
@@ -997,10 +1001,10 @@ static llvm::BasicBlock *createFinallyBlock(llvm::LLVMContext &context,
bufferToPrint.str(),
USE_GLOBAL_STR_CONSTS);
- llvm::SwitchInst *theSwitch = builder.CreateSwitch(builder.CreateLoad(
- *exceptionCaughtFlag),
- &terminatorBlock,
- 2);
+ llvm::SwitchInst *theSwitch = builder.CreateSwitch(
+ builder.CreateLoad(ourExceptionNotThrownState->getType(),
+ *exceptionCaughtFlag),
+ &terminatorBlock, 2);
theSwitch->addCase(ourExceptionCaughtState, &terminatorBlock);
theSwitch->addCase(ourExceptionThrownState, &unwindResumeBlock);
@@ -1185,8 +1189,9 @@ static llvm::Function *createCatchWrappedInvokeFunction(
llvm::Function *deleteOurException = module.getFunction("deleteOurException");
// Note: function handles NULL exceptions
- builder.CreateCall(deleteOurException,
- builder.CreateLoad(exceptionStorage));
+ builder.CreateCall(
+ deleteOurException,
+ builder.CreateLoad(builder.getInt8PtrTy(), exceptionStorage));
builder.CreateRetVoid();
// Normal Block
@@ -1206,7 +1211,8 @@ static llvm::Function *createCatchWrappedInvokeFunction(
builder.SetInsertPoint(unwindResumeBlock);
- builder.CreateResume(builder.CreateLoad(caughtResultStorage));
+ builder.CreateResume(
+ builder.CreateLoad(ourCaughtResultType, caughtResultStorage));
// Exception Block
@@ -1241,8 +1247,9 @@ static llvm::Function *createCatchWrappedInvokeFunction(
// Retrieve exception_class member from thrown exception
// (_Unwind_Exception instance). This member tells us whether or not
// the exception is foreign.
- llvm::Value *unwindExceptionClass =
- builder.CreateLoad(builder.CreateStructGEP(
+ llvm::Value *unwindExceptionClass = builder.CreateLoad(
+ builder.getInt64Ty(),
+ builder.CreateStructGEP(
ourUnwindExceptionType,
builder.CreatePointerCast(unwindException,
ourUnwindExceptionType->getPointerTo()),
@@ -1278,9 +1285,9 @@ static llvm::Function *createCatchWrappedInvokeFunction(
//
// Note: ourBaseFromUnwindOffset is usually negative
llvm::Value *typeInfoThrown = builder.CreatePointerCast(
- builder.CreateConstGEP1_64(unwindException,
- ourBaseFromUnwindOffset),
- ourExceptionType->getPointerTo());
+ builder.CreateConstGEP1_64(builder.getInt8PtrTy(), unwindException,
+ ourBaseFromUnwindOffset),
+ ourExceptionType->getPointerTo());
// Retrieve thrown exception type info type
//
@@ -1289,18 +1296,15 @@ static llvm::Function *createCatchWrappedInvokeFunction(
typeInfoThrown = builder.CreateStructGEP(ourExceptionType, typeInfoThrown, 0);
llvm::Value *typeInfoThrownType =
- builder.CreateStructGEP(builder.getInt8PtrTy(), typeInfoThrown, 0);
+ builder.CreateStructGEP(ourTypeInfoType, typeInfoThrown, 0);
- generateIntegerPrint(context,
- module,
- builder,
- *toPrint32Int,
- *(builder.CreateLoad(typeInfoThrownType)),
- "Gen: Exception type <%d> received (stack unwound) "
- " in " +
- ourId +
- ".\n",
- USE_GLOBAL_STR_CONSTS);
+ generateIntegerPrint(
+ context, module, builder, *toPrint32Int,
+ *(builder.CreateLoad(builder.getInt8Ty(), typeInfoThrownType)),
+ "Gen: Exception type <%d> received (stack unwound) "
+ " in " +
+ ourId + ".\n",
+ USE_GLOBAL_STR_CONSTS);
// Route to matched type info catch block or run cleanup finally block
llvm::SwitchInst *switchToCatchBlock = builder.CreateSwitch(retTypeInfoIndex,
@@ -1546,15 +1550,14 @@ typedef void (*OurExceptionThrowFunctType) (int32_t typeToThrow);
/// @param function generated test function to run
/// @param typeToThrow type info type of generated exception to throw, or
/// indicator to cause foreign exception to be thrown.
-static
-void runExceptionThrow(llvm::ExecutionEngine *engine,
- llvm::Function *function,
- int32_t typeToThrow) {
+static void runExceptionThrow(llvm::orc::LLJIT *JIT, std::string function,
+ int32_t typeToThrow) {
// Find test's function pointer
OurExceptionThrowFunctType functPtr =
- reinterpret_cast<OurExceptionThrowFunctType>(
- reinterpret_cast<intptr_t>(engine->getPointerToFunction(function)));
+ reinterpret_cast<OurExceptionThrowFunctType>(reinterpret_cast<uintptr_t>(
+ ExitOnErr(JIT->lookup(function))
+ .getValue()));
try {
// Run test
@@ -1583,7 +1586,6 @@ void runExceptionThrow(llvm::ExecutionEngine *engine,
// End test functions
//
-typedef llvm::ArrayRef<llvm::Type*> TypeArray;
/// This initialization routine creates type info globals and
/// adds external function declarations to module.
@@ -1894,93 +1896,81 @@ int main(int argc, char *argv[]) {
return(0);
}
- // If not set, exception handling will not be turned on
- llvm::TargetOptions Opts;
-
llvm::InitializeNativeTarget();
llvm::InitializeNativeTargetAsmPrinter();
- llvm::LLVMContext Context;
- llvm::IRBuilder<> theBuilder(Context);
+ auto Context = std::make_unique<llvm::LLVMContext>();
+ llvm::IRBuilder<> theBuilder(*Context);
// Make the module, which holds all the code.
std::unique_ptr<llvm::Module> Owner =
- std::make_unique<llvm::Module>("my cool jit", Context);
+ std::make_unique<llvm::Module>("my cool jit", *Context);
llvm::Module *module = Owner.get();
- std::unique_ptr<llvm::RTDyldMemoryManager> MemMgr(new llvm::SectionMemoryManager());
+ // Build LLJIT
+ std::unique_ptr<llvm::orc::LLJIT> JIT =
+ ExitOnErr(llvm::orc::LLJITBuilder().create());
- // Build engine with JIT
- llvm::EngineBuilder factory(std::move(Owner));
- factory.setEngineKind(llvm::EngineKind::JIT);
- factory.setTargetOptions(Opts);
- factory.setMCJITMemoryManager(std::move(MemMgr));
- llvm::ExecutionEngine *executionEngine = factory.create();
+ // Add automatic dynamic symbol search
+ auto &DyLib = JIT->getMainJITDylib();
+ auto JTMB = ExitOnErr(llvm::orc::JITTargetMachineBuilder::detectHost());
+ auto DL = ExitOnErr(JTMB.getDefaultDataLayoutForTarget());
+ DyLib.addGenerator(llvm::cantFail(
+ llvm::orc::DynamicLibrarySearchGenerator::GetForCurrentProcess(
+ DL.getGlobalPrefix())));
- {
- llvm::legacy::FunctionPassManager fpm(module);
+ // Set up the optimizer pipeline.
+ llvm::legacy::FunctionPassManager fpm(module);
- // Set up the optimizer pipeline.
- // Start with registering info about how the
- // target lays out data structures.
- module->setDataLayout(executionEngine->getDataLayout());
-
- // Optimizations turned on
+ // Optimizations turned on
#ifdef ADD_OPT_PASSES
- // Basic AliasAnslysis support for GVN.
- fpm.add(llvm::createBasicAliasAnalysisPass());
+ // Basic AliasAnslysis support for GVN.
+ fpm.add(llvm::createBasicAliasAnalysisPass());
- // Promote allocas to registers.
- fpm.add(llvm::createPromoteMemoryToRegisterPass());
+ // Promote allocas to registers.
+ fpm.add(llvm::createPromoteMemoryToRegisterPass());
- // Do simple "peephole" optimizations and bit-twiddling optzns.
- fpm.add(llvm::createInstructionCombiningPass());
+ // Do simple "peephole" optimizations and bit-twiddling optzns.
+ fpm.add(llvm::createInstructionCombiningPass());
- // Reassociate expressions.
- fpm.add(llvm::createReassociatePass());
+ // Reassociate expressions.
+ fpm.add(llvm::createReassociatePass());
- // Eliminate Common SubExpressions.
- fpm.add(llvm::createGVNPass());
+ // Eliminate Common SubExpressions.
+ fpm.add(llvm::createGVNPass());
- // Simplify the control flow graph (deleting unreachable
- // blocks, etc).
- fpm.add(llvm::createCFGSimplificationPass());
+ // Simplify the control flow graph (deleting unreachable
+ // blocks, etc).
+ fpm.add(llvm::createCFGSimplificationPass());
#endif // ADD_OPT_PASSES
- fpm.doInitialization();
+ fpm.doInitialization();
- // Generate test code using function throwCppException(...) as
- // the function which throws foreign exceptions.
- llvm::Function *toRun =
- createUnwindExceptionTest(*module,
- theBuilder,
- fpm,
- "throwCppException");
+ // Generate test code using function throwCppException(...) as
+ // the function which throws foreign exceptions.
+ createUnwindExceptionTest(*module, theBuilder, fpm, "throwCppException");
- executionEngine->finalizeObject();
+ ExitOnErr(JIT->addIRModule(
+ llvm::orc::ThreadSafeModule(std::move(Owner), std::move(Context))));
#ifndef NDEBUG
- fprintf(stderr, "\nBegin module dump:\n\n");
+ fprintf(stderr, "\nBegin module dump:\n\n");
- module->dump();
+ module->print(llvm::errs(), nullptr);
- fprintf(stderr, "\nEnd module dump:\n");
+ fprintf(stderr, "\nEnd module dump:\n");
#endif
- fprintf(stderr, "\n\nBegin Test:\n");
-
- for (int i = 1; i < argc; ++i) {
- // Run test for each argument whose value is the exception
- // type to throw.
- runExceptionThrow(executionEngine,
- toRun,
- (unsigned) strtoul(argv[i], NULL, 10));
- }
+ fprintf(stderr, "\n\nBegin Test:\n");
+ std::string toRun = "outerCatchFunct";
- fprintf(stderr, "\nEnd Test:\n\n");
+ for (int i = 1; i < argc; ++i) {
+ // Run test for each argument whose value is the exception
+ // type to throw.
+ runExceptionThrow(JIT.get(), toRun, (unsigned)strtoul(argv[i], NULL, 10));
}
- delete executionEngine;
+ fprintf(stderr, "\nEnd Test:\n\n");
return 0;
}
>From 66e699eead56eddfafc5b3cc731b25e8bfb6bb64 Mon Sep 17 00:00:00 2001
From: Eliaz Pitavy <eliaz.pitavy at obspm.fr>
Date: Wed, 18 Oct 2023 19:36:57 +0200
Subject: [PATCH 2/4] fixup! [ExceptionDemo] Transition example from MCJIT to
ORC and fix compiling errors (#63702)
---
llvm/examples/ExceptionDemo/ExceptionDemo.cpp | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/llvm/examples/ExceptionDemo/ExceptionDemo.cpp b/llvm/examples/ExceptionDemo/ExceptionDemo.cpp
index 97deb7b4393fbcb..50caa8856922511 100644
--- a/llvm/examples/ExceptionDemo/ExceptionDemo.cpp
+++ b/llvm/examples/ExceptionDemo/ExceptionDemo.cpp
@@ -1556,8 +1556,7 @@ static void runExceptionThrow(llvm::orc::LLJIT *JIT, std::string function,
// Find test's function pointer
OurExceptionThrowFunctType functPtr =
reinterpret_cast<OurExceptionThrowFunctType>(reinterpret_cast<uintptr_t>(
- ExitOnErr(JIT->lookup(function))
- .getValue()));
+ ExitOnErr(JIT->lookup(function)).getValue()));
try {
// Run test
>From e335617985ea3f4d36806a91e733e760d785e3b8 Mon Sep 17 00:00:00 2001
From: Eliaz Pitavy <eliaz.pitavy at obspm.fr>
Date: Wed, 18 Oct 2023 20:22:56 +0200
Subject: [PATCH 3/4] fixup! [ExceptionDemo] Transition example from MCJIT to
ORC and fix compiling errors (#63702)
---
llvm/examples/ExceptionDemo/ExceptionDemo.cpp | 1 -
1 file changed, 1 deletion(-)
diff --git a/llvm/examples/ExceptionDemo/ExceptionDemo.cpp b/llvm/examples/ExceptionDemo/ExceptionDemo.cpp
index 50caa8856922511..c66218d1561df07 100644
--- a/llvm/examples/ExceptionDemo/ExceptionDemo.cpp
+++ b/llvm/examples/ExceptionDemo/ExceptionDemo.cpp
@@ -1585,7 +1585,6 @@ static void runExceptionThrow(llvm::orc::LLJIT *JIT, std::string function,
// End test functions
//
-
/// This initialization routine creates type info globals and
/// adds external function declarations to module.
/// @param numTypeInfos number of linear type info associated type info types
>From 15891685d5e7fc2b9f01d49a1745bd2350f2d20c Mon Sep 17 00:00:00 2001
From: Eliaz Pitavy <eliaz.pitavy at obspm.fr>
Date: Mon, 13 Nov 2023 13:27:21 +0100
Subject: [PATCH 4/4] Remove process symbol search since it is the default
Signed-off-by: Eliaz Pitavy <eliaz.pitavy at obspm.fr>
---
llvm/examples/ExceptionDemo/ExceptionDemo.cpp | 8 --------
1 file changed, 8 deletions(-)
diff --git a/llvm/examples/ExceptionDemo/ExceptionDemo.cpp b/llvm/examples/ExceptionDemo/ExceptionDemo.cpp
index c66218d1561df07..d66aeeaf534c1ce 100644
--- a/llvm/examples/ExceptionDemo/ExceptionDemo.cpp
+++ b/llvm/examples/ExceptionDemo/ExceptionDemo.cpp
@@ -1908,14 +1908,6 @@ int main(int argc, char *argv[]) {
std::unique_ptr<llvm::orc::LLJIT> JIT =
ExitOnErr(llvm::orc::LLJITBuilder().create());
- // Add automatic dynamic symbol search
- auto &DyLib = JIT->getMainJITDylib();
- auto JTMB = ExitOnErr(llvm::orc::JITTargetMachineBuilder::detectHost());
- auto DL = ExitOnErr(JTMB.getDefaultDataLayoutForTarget());
- DyLib.addGenerator(llvm::cantFail(
- llvm::orc::DynamicLibrarySearchGenerator::GetForCurrentProcess(
- DL.getGlobalPrefix())));
-
// Set up the optimizer pipeline.
llvm::legacy::FunctionPassManager fpm(module);
More information about the llvm-commits
mailing list