Skip to content

Commit

Permalink
Fix -disable-exceptions flag (#581)
Browse files Browse the repository at this point in the history
  • Loading branch information
arshajii authored Aug 23, 2024
1 parent c214e2c commit 7b16b15
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
25 changes: 18 additions & 7 deletions codon/cir/llvm/llvisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1415,7 +1415,7 @@ int LLVMVisitor::getTypeIdx(types::Type *catchType) {
llvm::Value *LLVMVisitor::call(llvm::FunctionCallee callee,
llvm::ArrayRef<llvm::Value *> args) {
B->SetInsertPoint(block);
if (trycatch.empty()) {
if (trycatch.empty() || DisableExceptions) {
return B->CreateCall(callee, args);
} else {
auto *normalBlock = llvm::BasicBlock::Create(*context, "invoke.normal", func);
Expand Down Expand Up @@ -2877,7 +2877,11 @@ void LLVMVisitor::visit(const TryCatchFlow *x) {

// rethrow if uncaught
B->SetInsertPoint(unwindResumeBlock);
B->CreateResume(B->CreateLoad(padType, tc.catchStore));
if (DisableExceptions) {
B->CreateUnreachable();
} else {
B->CreateResume(B->CreateLoad(padType, tc.catchStore));
}

// make sure we delegate to parent try-catch if necessary
std::vector<types::Type *> catchTypesFull(tc.catchTypes);
Expand Down Expand Up @@ -2915,8 +2919,11 @@ void LLVMVisitor::visit(const TryCatchFlow *x) {

// exception handling
B->SetInsertPoint(tc.exceptionBlock);
llvm::LandingPadInst *caughtResult = B->CreateLandingPad(padType, catches.size());
caughtResult->setCleanup(true);
llvm::LandingPadInst *caughtResult = nullptr;
if (!DisableExceptions) {
caughtResult = B->CreateLandingPad(padType, catches.size());
caughtResult->setCleanup(true);
}
std::vector<llvm::Value *> typeIndices;

for (auto *catchType : catchTypesFull) {
Expand All @@ -2925,11 +2932,15 @@ void LLVMVisitor::visit(const TryCatchFlow *x) {
"codon.typeidx." + (catchType ? catchType->getName() : "<all>");
llvm::GlobalVariable *tidx = getTypeIdxVar(catchType);
typeIndices.push_back(tidx);
caughtResult->addClause(tidx);
if (caughtResult)
caughtResult->addClause(tidx);
}

llvm::Value *unwindException = B->CreateExtractValue(caughtResult, 0);
B->CreateStore(caughtResult, tc.catchStore);
llvm::Value *caughtResultOrUndef = caughtResult
? llvm::cast<llvm::Value>(caughtResult)
: llvm::UndefValue::get(padType);
auto *unwindException = B->CreateExtractValue(caughtResultOrUndef, 0);
B->CreateStore(caughtResultOrUndef, tc.catchStore);
B->CreateStore(excStateThrown, tc.excFlag);
llvm::Value *depthMax = B->getInt64(trycatch.size());
B->CreateStore(depthMax, tc.delegateDepth);
Expand Down
7 changes: 7 additions & 0 deletions docs/intro/releases.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ Upgraded to LLVM 17 (from 15).
- Several improvements to dynamic polymorphism to match CPython more
closely.

## New compiler options

- `-disable-exceptions` will disable exceptions, potentially eliding
various runtime checks (e.g. bounds checks for lists). This flag
should only be used if you know that no exceptions will be raised
in the given program.

# v0.16

## Python extensions
Expand Down

0 comments on commit 7b16b15

Please sign in to comment.