Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CIR][Interface] introduce CIRGlobalValueInterface for GlobalOp and FuncOp #641

Merged
merged 1 commit into from
May 31, 2024

Conversation

ghehg
Copy link
Collaborator

@ghehg ghehg commented May 30, 2024

CIRGlobalValueInterface inherits from mlir::Symbol as it should, and GlobalOp and FuncOp now has interface mlir::Symbol through CIRGlobalValueInterface

and this PR basically make function isDeclarationForLinker into the CIRGlobalValueInterface interface. We also change some call sites of isDeclaration to use CIRGlobalValueInterface when its appropriate.

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.

Thanks for improving this @ghehg, way to go. Some minor nits to address in inline comments.

While you are here, more changes you could do in followup PRs:

  • GetAddrOfGlobal should return the global value interface.
  • mlir::cir::isAvailableExternallyLinkage and friends from CIROpsEnums.h should be part of the interface instead of free functions (those were a hack because we didn't have an interface before).

"exception info",
"unsigned", "getNumArgOperands", (ins)>,
];
}
Copy link
Member

Choose a reason for hiding this comment

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

Nice formatting, thanks. Note that changes to CIRCallOpInterface are unrelated to the patch and makes it harder to review. Fine for this PR because you didn't know but please avoid doing that in the future, better do formatting/refactoring on their own PRs.


return isDeclaration();
bool hasAvailableExternallyLinkage() {
return mlir::cir::isAvailableExternallyLinkage(getLinkage());
Copy link
Member

Choose a reason for hiding this comment

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

Given both hasAvailableExternallyLinkage impls are equal, just make this the default impl in the td file, and delete the one from GlobalOp.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I actually tried that. But it fails to compile as tbgen doesn't recognize getLinkage(), and getLinkage() seems to be a getter for OP attribute "linkage". Would love to learn how we deal with this situation as I totally agree with you this code dup is ugly.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe something along the lines of what we see in CIR_CallOp could be doable:

    /// Return the callee of this operation
    CallInterfaceCallable getCallableForCallee() {
      return (*this)->getAttrOfType<SymbolRefAttr>("callee");
    }

Note that because follow up work will make mlir::cir::isAvailableExternallyLinkage be part of the interface as well, perhaps you can address this as part of that change instead.

@@ -2434,18 +2435,17 @@ void CIRGenModule::buildGlobalDecl(clang::GlobalDecl &D) {
// ways (e.g. by an extern inline function acquiring a strong function
// redefinition). Just ignore those cases.
// TODO: Not sure what to map this to for MLIR
Copy link
Member

Choose a reason for hiding this comment

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

You can delete the TODO as well. I think this whole change could be further simplified by:

auto cirGlobalValue = cast<mlir::cir::CIRGlobalValueInterface>(globalValueOp);
if (!cirGlobalValue.isDeclaration())
  return;

See the original code in CodeGenModule::EmitDeferred().

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Just to see if I understand it correctly. Does this mean "dyn_castmlir::cir::GetGlobalOp(Op)" should not be a valid check here? I.E. early logic in the function should make sure Op is of CIRGlobalValueInterface (either GlobalOp or FuncOp)
Actually, I had that doubt when I changed this code,
now as you suggested, after comparing with code in lib/clang/CodeGen/CodeGenModule.cpp
such as
void CodeGenModule::EmitDeferred() {
and CodeGenModule::GetAddrOfGlobalVar

I kinda question why CIR counterpart CIRGenModule::getAddrOfGlobalVar is returning GetGlobalOp insteadof GlobalOp?

Copy link
Member

Choose a reason for hiding this comment

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

My bad, I read GetGlobalOp as GlobalOp, I take back what I said :)

I kinda question why CIR counterpart CIRGenModule::getAddrOfGlobalVar is returning GetGlobalOp insteadof GlobalOp?

The OG one returns llvm::Constant *, which if we think in CIR terms, there isn't a good direct counterpart - it could be either and attribute (globalview) or an actual mlir::value result of getglobalop, so not really translatable at the time we wrote this.

If you have better ideas on how to go about this, it's definitely an improvement we could do in its own PR.

@@ -2434,18 +2435,17 @@ void CIRGenModule::buildGlobalDecl(clang::GlobalDecl &D) {
// ways (e.g. by an extern inline function acquiring a strong function
// redefinition). Just ignore those cases.
// TODO: Not sure what to map this to for MLIR
Copy link
Member

Choose a reason for hiding this comment

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

My bad, I read GetGlobalOp as GlobalOp, I take back what I said :)

I kinda question why CIR counterpart CIRGenModule::getAddrOfGlobalVar is returning GetGlobalOp insteadof GlobalOp?

The OG one returns llvm::Constant *, which if we think in CIR terms, there isn't a good direct counterpart - it could be either and attribute (globalview) or an actual mlir::value result of getglobalop, so not really translatable at the time we wrote this.

If you have better ideas on how to go about this, it's definitely an improvement we could do in its own PR.


return isDeclaration();
bool hasAvailableExternallyLinkage() {
return mlir::cir::isAvailableExternallyLinkage(getLinkage());
Copy link
Member

Choose a reason for hiding this comment

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

Maybe something along the lines of what we see in CIR_CallOp could be doable:

    /// Return the callee of this operation
    CallInterfaceCallable getCallableForCallee() {
      return (*this)->getAttrOfType<SymbolRefAttr>("callee");
    }

Note that because follow up work will make mlir::cir::isAvailableExternallyLinkage be part of the interface as well, perhaps you can address this as part of that change instead.

@bcardosolopes bcardosolopes merged commit e07b151 into llvm:main May 31, 2024
7 checks passed
bruteforceboy pushed a commit to bruteforceboy/clangir that referenced this pull request Oct 2, 2024
…uncOp (llvm#641)

CIRGlobalValueInterface inherits from mlir::Symbol as it should, and
GlobalOp and FuncOp now has interface mlir::Symbol through
CIRGlobalValueInterface

and this PR basically make function isDeclarationForLinker into the
CIRGlobalValueInterface interface. We also change some call sites of
isDeclaration to use CIRGlobalValueInterface when its appropriate.
Hugobros3 pushed a commit to shady-gang/clangir that referenced this pull request Oct 2, 2024
…uncOp (llvm#641)

CIRGlobalValueInterface inherits from mlir::Symbol as it should, and
GlobalOp and FuncOp now has interface mlir::Symbol through
CIRGlobalValueInterface

and this PR basically make function isDeclarationForLinker into the
CIRGlobalValueInterface interface. We also change some call sites of
isDeclaration to use CIRGlobalValueInterface when its appropriate.
smeenai pushed a commit to smeenai/clangir that referenced this pull request Oct 9, 2024
…uncOp (llvm#641)

CIRGlobalValueInterface inherits from mlir::Symbol as it should, and
GlobalOp and FuncOp now has interface mlir::Symbol through
CIRGlobalValueInterface

and this PR basically make function isDeclarationForLinker into the
CIRGlobalValueInterface interface. We also change some call sites of
isDeclaration to use CIRGlobalValueInterface when its appropriate.
keryell pushed a commit to keryell/clangir that referenced this pull request Oct 19, 2024
…uncOp (llvm#641)

CIRGlobalValueInterface inherits from mlir::Symbol as it should, and
GlobalOp and FuncOp now has interface mlir::Symbol through
CIRGlobalValueInterface

and this PR basically make function isDeclarationForLinker into the
CIRGlobalValueInterface interface. We also change some call sites of
isDeclaration to use CIRGlobalValueInterface when its appropriate.
lanza pushed a commit that referenced this pull request Nov 5, 2024
…uncOp (#641)

CIRGlobalValueInterface inherits from mlir::Symbol as it should, and
GlobalOp and FuncOp now has interface mlir::Symbol through
CIRGlobalValueInterface

and this PR basically make function isDeclarationForLinker into the
CIRGlobalValueInterface interface. We also change some call sites of
isDeclaration to use CIRGlobalValueInterface when its appropriate.
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.

2 participants