Skip to content

Commit 76f45e7

Browse files
Dev VMclaude
andcommitted
Fix IRBuilderHelper assertion when DtoDefineNakedFunction called at module scope
Use gIR->saveInsertPoint() instead of gIR->ir->saveIP() because the latter goes through IRBuilderHelper::operator->() which asserts that there's a valid insert block. At module scope (e.g., when compiling naked functions in phobos), there may not be an existing insert point. The RAII InsertPointGuard handles both null and non-null insert points correctly, saving and restoring the builder state automatically. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 9ca884a commit 76f45e7

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

gen/naked.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,13 @@ void DtoDefineNakedFunction(FuncDeclaration *fd) {
201201
llvm::BasicBlock *entryBB =
202202
llvm::BasicBlock::Create(gIR->context(), "entry", func);
203203

204-
// Save current insert point and switch to new function
205-
llvm::IRBuilderBase::InsertPoint savedIP = gIR->ir->saveIP();
206-
gIR->ir->SetInsertPoint(entryBB);
204+
// Save current insert point and switch to new function.
205+
// Use gIR->setInsertPoint() instead of gIR->ir->SetInsertPoint() because
206+
// the latter goes through IRBuilderHelper::operator->() which asserts that
207+
// there's a valid insert block. At module scope, there may not be one yet.
208+
// gIR->setInsertPoint() accesses the builder directly and also returns an
209+
// RAII guard that restores the previous state when it goes out of scope.
210+
const auto savedInsertPoint = gIR->setInsertPoint(entryBB);
207211

208212
// Clear the nakedAsm stream and collect the function body
209213
std::ostringstream &asmstr = gIR->nakedAsm;
@@ -252,8 +256,8 @@ void DtoDefineNakedFunction(FuncDeclaration *fd) {
252256
// Naked functions don't return normally through LLVM IR
253257
gIR->ir->CreateUnreachable();
254258

255-
// Restore insert point
256-
gIR->ir->restoreIP(savedIP);
259+
// The savedInsertPoint RAII guard automatically restores the insert point
260+
// when it goes out of scope.
257261

258262
// Handle DLL export on Windows
259263
if (global.params.dllexport ||

0 commit comments

Comments
 (0)