This repository was archived by the owner on Feb 21, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 196
[CIR] Implement static local variable initialization with guard variables #2046
Open
lanza
wants to merge
4
commits into
gh/lanza/20/base
Choose a base branch
from
gh/lanza/20/head
base: gh/lanza/20/base
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -636,8 +636,9 @@ CIRGenFunction::addInitializerToStaticVarDecl(const VarDecl &varDecl, | |
| else { | ||
| // Since we have a static initializer, this global variable can't | ||
| // be constant. | ||
| llvm_unreachable("C++ guarded init it NYI"); | ||
| globalOp.setConstant(false); | ||
| emitCXXGuardedInit(varDecl, globalOp, /*performInit*/ true); | ||
| getGlobalOp.setStaticLocal(true); | ||
| } | ||
| return globalOp; | ||
| } | ||
|
|
@@ -660,13 +661,34 @@ CIRGenFunction::addInitializerToStaticVarDecl(const VarDecl &varDecl, | |
| if (globalOp.getSymType() != typedInit.getType()) { | ||
| globalOp.setSymType(typedInit.getType()); | ||
|
|
||
| cir::GlobalOp oldGlobalOp = globalOp; | ||
| globalOp = | ||
| builder.createGlobal(CGM.getModule(), getLoc(varDecl.getSourceRange()), | ||
| oldGlobalOp.getName(), typedInit.getType(), | ||
| oldGlobalOp.getConstant(), globalOp.getLinkage()); | ||
| // FIXME(cir): OG codegen inserts new GV before old one, we probably don't | ||
| // need that? | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LLVM translation usually put them out in the proper order, because of the way MLIR handles this I don't think it should be a problem for us. |
||
| globalOp.setVisibility(oldGlobalOp.getVisibility()); | ||
| globalOp.setGlobalVisibilityAttr(oldGlobalOp.getGlobalVisibilityAttr()); | ||
| globalOp.setInitialValueAttr(typedInit); | ||
| globalOp.setTlsModelAttr(oldGlobalOp.getTlsModelAttr()); | ||
| globalOp.setDSOLocal(oldGlobalOp.getDsoLocal()); | ||
| assert(!cir::MissingFeatures::setComdat()); | ||
| assert(!cir::MissingFeatures::addressSpaceInGlobalVar()); | ||
|
|
||
| // Normally this should be done with a call to CGM.replaceGlobal(OldGV, GV), | ||
| // but since at this point the current block hasn't been really attached, | ||
| // there's no visibility into the GetGlobalOp corresponding to this Global. | ||
| // Given those constraints, thread in the GetGlobalOp and update it | ||
| // directly. | ||
| getGlobalOp.getAddr().setType(getBuilder().getPointerTo( | ||
| typedInit.getType(), globalOp.getAddrSpaceAttr())); | ||
|
|
||
| // Replace all uses of the old global with the new global | ||
| oldGlobalOp->replaceAllUsesWith(globalOp); | ||
|
|
||
| // Erase the old global, since it is no longer used. | ||
| oldGlobalOp->erase(); | ||
| } | ||
|
|
||
| bool needsDtor = | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2243,6 +2243,19 @@ class CIRGenFunction : public CIRGenTypeCache { | |
|
|
||
| void emitInvariantStart(CharUnits Size); | ||
|
|
||
| /// Emit code in this function to perform a guarded variable | ||
| /// initialization. Guarded initializations are used when it's not | ||
| /// possible to prove that an initialization will be done exactly | ||
| /// once, e.g. with a static local variable or a static data member | ||
| /// of a class template. | ||
| void emitCXXGuardedInit(const VarDecl &varDecl, cir::GlobalOp globalOp, | ||
| bool performInit); | ||
|
|
||
| /// EmitCXXGlobalVarDeclInit - Create the initializer for a C++ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the |
||
| /// variable with global storage. | ||
| void emitCXXGlobalVarDeclInit(const VarDecl &varDecl, cir::GlobalOp globalOp, | ||
| bool performInit); | ||
|
|
||
| mlir::LogicalResult emitLabel(const clang::LabelDecl *D); | ||
| mlir::LogicalResult emitLabelStmt(const clang::LabelStmt &S); | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.