Skip to content

Commit 7095b8e

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 <[email protected]>
1 parent 9ca884a commit 7095b8e

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

gen/naked.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,11 @@ 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();
204+
// Save current insert point and switch to new function.
205+
// Use gIR->saveInsertPoint() instead of gIR->ir->saveIP() because the latter
206+
// goes through IRBuilderHelper::operator->() which asserts that there's a
207+
// valid insert block. At module scope, there may not be one yet.
208+
const auto savedInsertPoint = gIR->saveInsertPoint();
206209
gIR->ir->SetInsertPoint(entryBB);
207210

208211
// Clear the nakedAsm stream and collect the function body
@@ -252,8 +255,8 @@ void DtoDefineNakedFunction(FuncDeclaration *fd) {
252255
// Naked functions don't return normally through LLVM IR
253256
gIR->ir->CreateUnreachable();
254257

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

258261
// Handle DLL export on Windows
259262
if (global.params.dllexport ||

0 commit comments

Comments
 (0)