Skip to content

Commit b59760d

Browse files
Revert "[ExceptionDemo] Correct and update example ExceptionDemo" (#92257)
Reverts #69485
1 parent f8395f8 commit b59760d

File tree

2 files changed

+108
-77
lines changed

2 files changed

+108
-77
lines changed

llvm/examples/ExceptionDemo/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
set(LLVM_LINK_COMPONENTS
22
Core
33
ExecutionEngine
4-
ORCJIT
4+
MC
5+
MCJIT
6+
RuntimeDyld
57
Support
68
Target
79
nativecodegen

llvm/examples/ExceptionDemo/ExceptionDemo.cpp

Lines changed: 105 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,8 @@
4949

5050
#include "llvm/ADT/STLExtras.h"
5151
#include "llvm/BinaryFormat/Dwarf.h"
52-
#include "llvm/ExecutionEngine/Orc/Core.h"
53-
#include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
54-
#include "llvm/ExecutionEngine/Orc/LLJIT.h"
52+
#include "llvm/ExecutionEngine/MCJIT.h"
53+
#include "llvm/ExecutionEngine/SectionMemoryManager.h"
5554
#include "llvm/IR/DataLayout.h"
5655
#include "llvm/IR/DerivedTypes.h"
5756
#include "llvm/IR/IRBuilder.h"
@@ -85,8 +84,6 @@
8584
#define USE_GLOBAL_STR_CONSTS true
8685
#endif
8786

88-
llvm::ExitOnError ExitOnErr;
89-
9087
//
9188
// Example types
9289
//
@@ -145,7 +142,6 @@ static llvm::ConstantInt *ourExceptionCaughtState;
145142

146143
typedef std::vector<std::string> ArgNames;
147144
typedef std::vector<llvm::Type*> ArgTypes;
148-
typedef llvm::ArrayRef<llvm::Type *> TypeArray;
149145

150146
//
151147
// Code Generation Utilities
@@ -896,10 +892,13 @@ void generateStringPrint(llvm::LLVMContext &context,
896892
/// generated, and is used to hold the constant string. A value of
897893
/// false indicates that the constant string will be stored on the
898894
/// stack.
899-
void generateIntegerPrint(llvm::LLVMContext &context, llvm::Module &module,
895+
void generateIntegerPrint(llvm::LLVMContext &context,
896+
llvm::Module &module,
900897
llvm::IRBuilder<> &builder,
901-
llvm::Function &printFunct, llvm::Value *toPrint,
902-
std::string format, bool useGlobal = true) {
898+
llvm::Function &printFunct,
899+
llvm::Value &toPrint,
900+
std::string format,
901+
bool useGlobal = true) {
903902
llvm::Constant *stringConstant =
904903
llvm::ConstantDataArray::getString(context, format);
905904
llvm::Value *stringVar;
@@ -921,9 +920,10 @@ void generateIntegerPrint(llvm::LLVMContext &context, llvm::Module &module,
921920

922921
llvm::Value *cast = builder.CreateBitCast(stringVar,
923922
builder.getPtrTy());
924-
builder.CreateCall(&printFunct, {toPrint, cast});
923+
builder.CreateCall(&printFunct, {&toPrint, cast});
925924
}
926925

926+
927927
/// Generates code to handle finally block type semantics: always runs
928928
/// regardless of whether a thrown exception is passing through or the
929929
/// parent function is simply exiting. In addition to printing some state
@@ -997,10 +997,10 @@ static llvm::BasicBlock *createFinallyBlock(llvm::LLVMContext &context,
997997
bufferToPrint.str(),
998998
USE_GLOBAL_STR_CONSTS);
999999

1000-
llvm::SwitchInst *theSwitch = builder.CreateSwitch(
1001-
builder.CreateLoad(ourExceptionNotThrownState->getType(),
1002-
*exceptionCaughtFlag),
1003-
&terminatorBlock, 2);
1000+
llvm::SwitchInst *theSwitch = builder.CreateSwitch(builder.CreateLoad(
1001+
*exceptionCaughtFlag),
1002+
&terminatorBlock,
1003+
2);
10041004
theSwitch->addCase(ourExceptionCaughtState, &terminatorBlock);
10051005
theSwitch->addCase(ourExceptionThrownState, &unwindResumeBlock);
10061006

@@ -1186,7 +1186,7 @@ static llvm::Function *createCatchWrappedInvokeFunction(
11861186

11871187
// Note: function handles NULL exceptions
11881188
builder.CreateCall(deleteOurException,
1189-
builder.CreateLoad(builder.getPtrTy(), exceptionStorage));
1189+
builder.CreateLoad(exceptionStorage));
11901190
builder.CreateRetVoid();
11911191

11921192
// Normal Block
@@ -1206,8 +1206,7 @@ static llvm::Function *createCatchWrappedInvokeFunction(
12061206

12071207
builder.SetInsertPoint(unwindResumeBlock);
12081208

1209-
builder.CreateResume(
1210-
builder.CreateLoad(ourCaughtResultType, caughtResultStorage));
1209+
builder.CreateResume(builder.CreateLoad(caughtResultStorage));
12111210

12121211
// Exception Block
12131212

@@ -1242,9 +1241,8 @@ static llvm::Function *createCatchWrappedInvokeFunction(
12421241
// Retrieve exception_class member from thrown exception
12431242
// (_Unwind_Exception instance). This member tells us whether or not
12441243
// the exception is foreign.
1245-
llvm::Value *unwindExceptionClass = builder.CreateLoad(
1246-
builder.getInt64Ty(),
1247-
builder.CreateStructGEP(
1244+
llvm::Value *unwindExceptionClass =
1245+
builder.CreateLoad(builder.CreateStructGEP(
12481246
ourUnwindExceptionType,
12491247
builder.CreatePointerCast(unwindException,
12501248
ourUnwindExceptionType->getPointerTo()),
@@ -1280,9 +1278,9 @@ static llvm::Function *createCatchWrappedInvokeFunction(
12801278
//
12811279
// Note: ourBaseFromUnwindOffset is usually negative
12821280
llvm::Value *typeInfoThrown = builder.CreatePointerCast(
1283-
builder.CreateConstGEP1_64(builder.getPtrTy(), unwindException,
1284-
ourBaseFromUnwindOffset),
1285-
ourExceptionType->getPointerTo());
1281+
builder.CreateConstGEP1_64(unwindException,
1282+
ourBaseFromUnwindOffset),
1283+
ourExceptionType->getPointerTo());
12861284

12871285
// Retrieve thrown exception type info type
12881286
//
@@ -1291,15 +1289,17 @@ static llvm::Function *createCatchWrappedInvokeFunction(
12911289
typeInfoThrown = builder.CreateStructGEP(ourExceptionType, typeInfoThrown, 0);
12921290

12931291
llvm::Value *typeInfoThrownType =
1294-
builder.CreateStructGEP(ourTypeInfoType, typeInfoThrown, 0);
1292+
builder.CreateStructGEP(builder.getPtrTy(), typeInfoThrown, 0);
12951293

1296-
llvm::Value *ti8 =
1297-
builder.CreateLoad(builder.getInt8Ty(), typeInfoThrownType);
1298-
generateIntegerPrint(context, module, builder, *toPrint32Int,
1299-
builder.CreateZExt(ti8, builder.getInt32Ty()),
1294+
generateIntegerPrint(context,
1295+
module,
1296+
builder,
1297+
*toPrint32Int,
1298+
*(builder.CreateLoad(typeInfoThrownType)),
13001299
"Gen: Exception type <%d> received (stack unwound) "
13011300
" in " +
1302-
ourId + ".\n",
1301+
ourId +
1302+
".\n",
13031303
USE_GLOBAL_STR_CONSTS);
13041304

13051305
// Route to matched type info catch block or run cleanup finally block
@@ -1311,7 +1311,8 @@ static llvm::Function *createCatchWrappedInvokeFunction(
13111311

13121312
for (unsigned i = 1; i <= numExceptionsToCatch; ++i) {
13131313
nextTypeToCatch = i - 1;
1314-
switchToCatchBlock->addCase(llvm::ConstantInt::get(builder.getInt32Ty(), i),
1314+
switchToCatchBlock->addCase(llvm::ConstantInt::get(
1315+
llvm::Type::getInt32Ty(context), i),
13151316
catchBlocks[nextTypeToCatch]);
13161317
}
13171318

@@ -1386,10 +1387,14 @@ createThrowExceptionFunction(llvm::Module &module, llvm::IRBuilder<> &builder,
13861387
builder.SetInsertPoint(entryBlock);
13871388

13881389
llvm::Function *toPrint32Int = module.getFunction("print32Int");
1389-
generateIntegerPrint(context, module, builder, *toPrint32Int,
1390-
builder.CreateZExt(exceptionType, builder.getInt32Ty()),
1391-
"\nGen: About to throw exception type <%d> in " + ourId +
1392-
".\n",
1390+
generateIntegerPrint(context,
1391+
module,
1392+
builder,
1393+
*toPrint32Int,
1394+
*exceptionType,
1395+
"\nGen: About to throw exception type <%d> in " +
1396+
ourId +
1397+
".\n",
13931398
USE_GLOBAL_STR_CONSTS);
13941399

13951400
// Switches on runtime type info type value to determine whether or not
@@ -1541,13 +1546,15 @@ typedef void (*OurExceptionThrowFunctType) (int32_t typeToThrow);
15411546
/// @param function generated test function to run
15421547
/// @param typeToThrow type info type of generated exception to throw, or
15431548
/// indicator to cause foreign exception to be thrown.
1544-
static void runExceptionThrow(llvm::orc::LLJIT *JIT, std::string function,
1545-
int32_t typeToThrow) {
1549+
static
1550+
void runExceptionThrow(llvm::ExecutionEngine *engine,
1551+
llvm::Function *function,
1552+
int32_t typeToThrow) {
15461553

15471554
// Find test's function pointer
15481555
OurExceptionThrowFunctType functPtr =
1549-
reinterpret_cast<OurExceptionThrowFunctType>(reinterpret_cast<uintptr_t>(
1550-
ExitOnErr(JIT->lookup(function)).getValue()));
1556+
reinterpret_cast<OurExceptionThrowFunctType>(
1557+
reinterpret_cast<intptr_t>(engine->getPointerToFunction(function)));
15511558

15521559
try {
15531560
// Run test
@@ -1576,6 +1583,8 @@ static void runExceptionThrow(llvm::orc::LLJIT *JIT, std::string function,
15761583
// End test functions
15771584
//
15781585

1586+
typedef llvm::ArrayRef<llvm::Type*> TypeArray;
1587+
15791588
/// This initialization routine creates type info globals and
15801589
/// adds external function declarations to module.
15811590
/// @param numTypeInfos number of linear type info associated type info types
@@ -1885,73 +1894,93 @@ int main(int argc, char *argv[]) {
18851894
return(0);
18861895
}
18871896

1897+
// If not set, exception handling will not be turned on
1898+
llvm::TargetOptions Opts;
1899+
18881900
llvm::InitializeNativeTarget();
18891901
llvm::InitializeNativeTargetAsmPrinter();
1890-
auto Context = std::make_unique<llvm::LLVMContext>();
1891-
llvm::IRBuilder<> theBuilder(*Context);
1902+
llvm::LLVMContext Context;
1903+
llvm::IRBuilder<> theBuilder(Context);
18921904

18931905
// Make the module, which holds all the code.
18941906
std::unique_ptr<llvm::Module> Owner =
1895-
std::make_unique<llvm::Module>("my cool jit", *Context);
1907+
std::make_unique<llvm::Module>("my cool jit", Context);
18961908
llvm::Module *module = Owner.get();
18971909

1898-
// Build LLJIT
1899-
std::unique_ptr<llvm::orc::LLJIT> JIT =
1900-
ExitOnErr(llvm::orc::LLJITBuilder().create());
1910+
std::unique_ptr<llvm::RTDyldMemoryManager> MemMgr(new llvm::SectionMemoryManager());
19011911

1902-
// Set up the optimizer pipeline.
1903-
llvm::legacy::FunctionPassManager fpm(module);
1912+
// Build engine with JIT
1913+
llvm::EngineBuilder factory(std::move(Owner));
1914+
factory.setEngineKind(llvm::EngineKind::JIT);
1915+
factory.setTargetOptions(Opts);
1916+
factory.setMCJITMemoryManager(std::move(MemMgr));
1917+
llvm::ExecutionEngine *executionEngine = factory.create();
19041918

1905-
// Optimizations turned on
1919+
{
1920+
llvm::legacy::FunctionPassManager fpm(module);
1921+
1922+
// Set up the optimizer pipeline.
1923+
// Start with registering info about how the
1924+
// target lays out data structures.
1925+
module->setDataLayout(executionEngine->getDataLayout());
1926+
1927+
// Optimizations turned on
19061928
#ifdef ADD_OPT_PASSES
19071929

1908-
// Basic AliasAnslysis support for GVN.
1909-
fpm.add(llvm::createBasicAliasAnalysisPass());
1930+
// Basic AliasAnslysis support for GVN.
1931+
fpm.add(llvm::createBasicAliasAnalysisPass());
19101932

1911-
// Promote allocas to registers.
1912-
fpm.add(llvm::createPromoteMemoryToRegisterPass());
1933+
// Promote allocas to registers.
1934+
fpm.add(llvm::createPromoteMemoryToRegisterPass());
19131935

1914-
// Do simple "peephole" optimizations and bit-twiddling optzns.
1915-
fpm.add(llvm::createInstructionCombiningPass());
1936+
// Do simple "peephole" optimizations and bit-twiddling optzns.
1937+
fpm.add(llvm::createInstructionCombiningPass());
19161938

1917-
// Reassociate expressions.
1918-
fpm.add(llvm::createReassociatePass());
1939+
// Reassociate expressions.
1940+
fpm.add(llvm::createReassociatePass());
19191941

1920-
// Eliminate Common SubExpressions.
1921-
fpm.add(llvm::createGVNPass());
1942+
// Eliminate Common SubExpressions.
1943+
fpm.add(llvm::createGVNPass());
19221944

1923-
// Simplify the control flow graph (deleting unreachable
1924-
// blocks, etc).
1925-
fpm.add(llvm::createCFGSimplificationPass());
1945+
// Simplify the control flow graph (deleting unreachable
1946+
// blocks, etc).
1947+
fpm.add(llvm::createCFGSimplificationPass());
19261948
#endif // ADD_OPT_PASSES
19271949

1928-
fpm.doInitialization();
1950+
fpm.doInitialization();
19291951

1930-
// Generate test code using function throwCppException(...) as
1931-
// the function which throws foreign exceptions.
1932-
createUnwindExceptionTest(*module, theBuilder, fpm, "throwCppException");
1952+
// Generate test code using function throwCppException(...) as
1953+
// the function which throws foreign exceptions.
1954+
llvm::Function *toRun =
1955+
createUnwindExceptionTest(*module,
1956+
theBuilder,
1957+
fpm,
1958+
"throwCppException");
19331959

1934-
ExitOnErr(JIT->addIRModule(
1935-
llvm::orc::ThreadSafeModule(std::move(Owner), std::move(Context))));
1960+
executionEngine->finalizeObject();
19361961

19371962
#ifndef NDEBUG
1938-
fprintf(stderr, "\nBegin module dump:\n\n");
1963+
fprintf(stderr, "\nBegin module dump:\n\n");
19391964

1940-
module->print(llvm::errs(), nullptr);
1965+
module->dump();
19411966

1942-
fprintf(stderr, "\nEnd module dump:\n");
1967+
fprintf(stderr, "\nEnd module dump:\n");
19431968
#endif
19441969

1945-
fprintf(stderr, "\n\nBegin Test:\n");
1946-
std::string toRun = "outerCatchFunct";
1970+
fprintf(stderr, "\n\nBegin Test:\n");
1971+
1972+
for (int i = 1; i < argc; ++i) {
1973+
// Run test for each argument whose value is the exception
1974+
// type to throw.
1975+
runExceptionThrow(executionEngine,
1976+
toRun,
1977+
(unsigned) strtoul(argv[i], NULL, 10));
1978+
}
19471979

1948-
for (int i = 1; i < argc; ++i) {
1949-
// Run test for each argument whose value is the exception
1950-
// type to throw.
1951-
runExceptionThrow(JIT.get(), toRun, (unsigned)strtoul(argv[i], NULL, 10));
1980+
fprintf(stderr, "\nEnd Test:\n\n");
19521981
}
19531982

1954-
fprintf(stderr, "\nEnd Test:\n\n");
1983+
delete executionEngine;
19551984

19561985
return 0;
19571986
}

0 commit comments

Comments
 (0)