Skip to content

Conversation

@seven-mile
Copy link
Collaborator

As noted in this comment, the nested if-arms in GlobalOpLowering are somewhat confusing and error-prone. This PR simplifies the logic into more straightforward components.

Since LLVM's GlobalOp accepts two types of initializers (either an initializer value or an initializer region), we've extracted the decision logic into a separate function called lowerInitializer. This function takes two inout arguments: mlir::Attribute &init (for the attribute value) and bool useInitializerRegion (as the decision indicator). All code paths then converge at a common epilogue that handles the operation rewriting.

The previous implementation for lowering DataMemberAttr initializers relied on recursion between MLIR rewrite calls, which made the control flow somewhat opaque. The new version makes this explicit by using a clear self-recursive pattern within lowerInitializer.

@bcardosolopes
Copy link
Member

The previous implementation for lowering DataMemberAttr initializers relied on recursion between MLIR rewrite calls

Perhaps at some point we should change this to some iteration algorithm instead.

Copy link
Member

@bcardosolopes bcardosolopes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks for the cleanup!

@bcardosolopes bcardosolopes merged commit cb15d3f into main Mar 25, 2025
9 checks passed
terapines-osc-cir pushed a commit to Terapines/clangir that referenced this pull request Sep 2, 2025
…ity and maintainability (llvm#1525)

As noted in [this
comment](llvm#1442 (comment)),
the nested if-arms in `GlobalOpLowering` are somewhat confusing and
error-prone. This PR simplifies the logic into more straightforward
components.

Since LLVM's GlobalOp accepts two types of initializers (either an
initializer value or an initializer region), we've extracted the
decision logic into a separate function called `lowerInitializer`. This
function takes two inout arguments: `mlir::Attribute &init` (for the
attribute value) and `bool useInitializerRegion` (as the decision
indicator). All code paths then converge at a common epilogue that
handles the operation rewriting.

The previous implementation for lowering `DataMemberAttr` initializers
relied on recursion between MLIR rewrite calls, which made the control
flow somewhat opaque. The new version makes this explicit by using a
clear self-recursive pattern within `lowerInitializer`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants