-
Notifications
You must be signed in to change notification settings - Fork 98
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,24 +10,49 @@ | |
#define MLIR_CIR_OP_INTERFACES | ||
|
||
include "mlir/IR/OpBase.td" | ||
include "mlir/IR/SymbolInterfaces.td" | ||
include "mlir/Interfaces/CallInterfaces.td" | ||
|
||
let cppNamespace = "::mlir::cir" in { | ||
// The CIRCallOpInterface must be used instead of CallOpInterface when looking | ||
// at arguments and other bits of CallOp. This creates a level of abstraction | ||
// that's useful for handling indirect calls and other details. | ||
def CIRCallOpInterface : OpInterface<"CIRCallOpInterface", [CallOpInterface]> { | ||
def CIRCallOpInterface | ||
: OpInterface<"CIRCallOpInterface", [CallOpInterface]> { | ||
let methods = [ | ||
InterfaceMethod<"", "mlir::Operation::operand_iterator", | ||
"arg_operand_begin", (ins)>, | ||
InterfaceMethod<"", "mlir::Operation::operand_iterator", | ||
"arg_operand_end", (ins)>, | ||
InterfaceMethod< | ||
"Return the operand at index 'i', accounts for indirect call or " | ||
"exception info", "mlir::Value", "getArgOperand", (ins "unsigned":$i)>, | ||
"Return the operand at index 'i', accounts for indirect call or " | ||
"exception info", | ||
"mlir::Value", "getArgOperand", | ||
(ins "unsigned" | ||
: $i)>, | ||
InterfaceMethod< | ||
"Return the number of operands, accounts for indirect call or " | ||
"exception info", "unsigned", "getNumArgOperands", (ins)>, | ||
"Return the number of operands, accounts for indirect call or " | ||
"exception info", | ||
"unsigned", "getNumArgOperands", (ins)>, | ||
]; | ||
} | ||
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. Nice formatting, thanks. Note that changes to |
||
|
||
def CIRGlobalValueInterface | ||
: OpInterface<"CIRGlobalValueInterface", [Symbol]> { | ||
|
||
let methods = [ | ||
InterfaceMethod<"", | ||
"bool", "hasAvailableExternallyLinkage", (ins), [{}], | ||
/*defaultImplementation=*/[{ return false; }] | ||
>, | ||
InterfaceMethod<"", | ||
"bool", "isDeclarationForLinker", (ins), [{}], | ||
/*defaultImplementation=*/[{ | ||
if ($_op.hasAvailableExternallyLinkage()) | ||
return true; | ||
return $_op.isDeclaration(); | ||
}] | ||
>, | ||
]; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -469,11 +469,12 @@ void CIRGenModule::buildGlobalFunctionDefinition(GlobalDecl GD, | |
Op = GetAddrOfFunction(GD, Ty, /*ForVTable=*/false, /*DontDefer=*/true, | ||
ForDefinition); | ||
|
||
auto Fn = cast<mlir::cir::FuncOp>(Op); | ||
// Already emitted. | ||
if (!Fn.isDeclaration()) | ||
auto globalVal = dyn_cast_or_null<mlir::cir::CIRGlobalValueInterface>(Op); | ||
if (globalVal && !globalVal.isDeclaration()) { | ||
// Already emitted. | ||
return; | ||
|
||
} | ||
auto Fn = cast<mlir::cir::FuncOp>(Op); | ||
setFunctionLinkage(GD, Fn); | ||
setGVProperties(Op, D); | ||
// TODO(cir): MaubeHandleStaticInExternC | ||
|
@@ -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 | ||
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. You can delete the TODO as well. I think this whole change could be further simplified by:
See the original code in 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. 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) I kinda question why CIR counterpart CIRGenModule::getAddrOfGlobalVar is returning GetGlobalOp insteadof GlobalOp? 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. My bad, I read
The OG one returns If you have better ideas on how to go about this, it's definitely an improvement we could do in its own PR. |
||
if (auto Fn = dyn_cast<mlir::cir::FuncOp>(Op)) | ||
if (!Fn.isDeclaration()) | ||
return; | ||
|
||
// TODO(cir): create a global value trait that allow us to uniformly handle | ||
// global variables and functions. | ||
auto globalValueOp = Op; | ||
if (auto Gv = dyn_cast<mlir::cir::GetGlobalOp>(Op)) { | ||
auto *result = | ||
mlir::SymbolTable::lookupSymbolIn(getModule(), Gv.getNameAttr()); | ||
if (auto globalOp = dyn_cast<mlir::cir::GlobalOp>(result)) | ||
if (!globalOp.isDeclaration()) | ||
return; | ||
globalValueOp = result; | ||
} | ||
|
||
if (auto cirGlobalValue = | ||
dyn_cast<mlir::cir::CIRGlobalValueInterface>(globalValueOp)) { | ||
if (!cirGlobalValue.isDeclaration()) | ||
return; | ||
} | ||
|
||
// If this is OpenMP, check if it is legal to emit this global normally. | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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: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.