-
Notifications
You must be signed in to change notification settings - Fork 13.4k
[Flang][OpenMP] Add global address space to globals for target device #119585
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
base: main
Are you sure you want to change the base?
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -134,16 +134,65 @@ addLLVMOpBundleAttrs(mlir::ConversionPatternRewriter &rewriter, | |||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
namespace { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Creates an existing operation with an AddressOfOp or an AddrSpaceCastOp | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
// depending on the existing address spaces of the type. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
mlir::Value createAddrOfOrASCast(mlir::ConversionPatternRewriter &rewriter, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
mlir::Location loc, std::uint64_t globalAS, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
std::uint64_t programAS, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
llvm::StringRef symName, mlir::Type type) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (mlir::isa<mlir::LLVM::LLVMPointerType>(type)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (globalAS != programAS) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
auto llvmAddrOp = rewriter.create<mlir::LLVM::AddressOfOp>( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
loc, getLlvmPtrType(rewriter.getContext(), globalAS), symName); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
return rewriter.create<mlir::LLVM::AddrSpaceCastOp>( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
loc, getLlvmPtrType(rewriter.getContext(), programAS), llvmAddrOp); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
return rewriter.create<mlir::LLVM::AddressOfOp>( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
loc, getLlvmPtrType(rewriter.getContext(), globalAS), symName); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
return rewriter.create<mlir::LLVM::AddressOfOp>(loc, type, symName); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
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. The 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. Split it into two seperate functions, hopefully it's a little more readable and is what you expected! :-) |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Replaces an existing operation with an AddressOfOp or an AddrSpaceCastOp | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
// depending on the existing address spaces of the type. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
mlir::Value replaceWithAddrOfOrASCast(mlir::ConversionPatternRewriter &rewriter, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
mlir::Location loc, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
std::uint64_t globalAS, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
std::uint64_t programAS, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
llvm::StringRef symName, mlir::Type type, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
mlir::Operation *replaceOp) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (mlir::isa<mlir::LLVM::LLVMPointerType>(type)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (globalAS != programAS) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
auto llvmAddrOp = rewriter.create<mlir::LLVM::AddressOfOp>( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
loc, getLlvmPtrType(rewriter.getContext(), globalAS), symName); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
return rewriter.replaceOpWithNewOp<mlir::LLVM::AddrSpaceCastOp>( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
replaceOp, ::getLlvmPtrType(rewriter.getContext(), programAS), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
llvmAddrOp); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
return rewriter.replaceOpWithNewOp<mlir::LLVM::AddressOfOp>( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
replaceOp, getLlvmPtrType(rewriter.getContext(), globalAS), symName); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
return rewriter.replaceOpWithNewOp<mlir::LLVM::AddressOfOp>(replaceOp, type, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
symName); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+165
to
+177
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. Nit: Feel free to disagree, but it seems like the logic here could be simplified a bit with something like this:
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
/// Lower `fir.address_of` operation to `llvm.address_of` operation. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
struct AddrOfOpConversion : public fir::FIROpConversion<fir::AddrOfOp> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
using FIROpConversion::FIROpConversion; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
llvm::LogicalResult | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
matchAndRewrite(fir::AddrOfOp addr, OpAdaptor adaptor, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
mlir::ConversionPatternRewriter &rewriter) const override { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
auto ty = convertType(addr.getType()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
rewriter.replaceOpWithNewOp<mlir::LLVM::AddressOfOp>( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
addr, ty, addr.getSymbol().getRootReference().getValue()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
auto global = addr->getParentOfType<mlir::ModuleOp>() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
.lookupSymbol<mlir::LLVM::GlobalOp>(addr.getSymbol()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
replaceWithAddrOfOrASCast( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
rewriter, addr->getLoc(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
global ? global.getAddrSpace() : getGlobalAddressSpace(rewriter), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
getProgramAddressSpace(rewriter), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
global ? global.getSymName() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
: addr.getSymbol().getRootReference().getValue(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
convertType(addr.getType()), addr); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
return mlir::success(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -1350,14 +1399,34 @@ struct EmboxCommonConversion : public fir::FIROpConversion<OP> { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
? fir::NameUniquer::getTypeDescriptorAssemblyName(recType.getName()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
: fir::NameUniquer::getTypeDescriptorName(recType.getName()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
mlir::Type llvmPtrTy = ::getLlvmPtrType(mod.getContext()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
// We allow the module to be set to a default layout if it's a regular module | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
// however, we prevent this if it's a GPU module, as the datalayout in these | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
// cases will currently be the union of the GPU Module and the parent builtin | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
// module, with the GPU module overriding the parent where there are duplicates. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
// However, if we force the default layout onto a GPU module, with no datalayout | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
// it'll result in issues as the infrastructure does not support the union of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
// two layouts with builtin data layout entries currently (and it doesn't look | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
// like it was intended to). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+1403
to
+1410
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.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
std::optional<mlir::DataLayout> dataLayout = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
fir::support::getOrSetMLIRDataLayout( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
mod, /*allowDefaultLayout*/ mlir::isa<mlir::gpu::GPUModuleOp>(mod) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
? false | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
: true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+1413
to
+1415
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.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert(dataLayout.has_value() && "Module missing DataLayout information"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (auto global = mod.template lookupSymbol<fir::GlobalOp>(name)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
return rewriter.create<mlir::LLVM::AddressOfOp>(loc, llvmPtrTy, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
global.getSymName()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
return createAddrOfOrASCast( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
rewriter, loc, fir::factory::getGlobalAddressSpace(&*dataLayout), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
fir::factory::getProgramAddressSpace(&*dataLayout), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
global.getSymName(), llvmPtrTy); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (auto global = mod.template lookupSymbol<mlir::LLVM::GlobalOp>(name)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
// The global may have already been translated to LLVM. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
return rewriter.create<mlir::LLVM::AddressOfOp>(loc, llvmPtrTy, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
global.getSymName()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
return createAddrOfOrASCast( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
rewriter, loc, global.getAddrSpace(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
fir::factory::getProgramAddressSpace(&*dataLayout), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
global.getSymName(), llvmPtrTy); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Type info derived types do not have type descriptors since they are the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
// types defining type descriptors. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -2896,12 +2965,16 @@ struct TypeDescOpConversion : public fir::FIROpConversion<fir::TypeDescOp> { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
: fir::NameUniquer::getTypeDescriptorName(recordType.getName()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
auto llvmPtrTy = ::getLlvmPtrType(typeDescOp.getContext()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (auto global = module.lookupSymbol<mlir::LLVM::GlobalOp>(typeDescName)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
rewriter.replaceOpWithNewOp<mlir::LLVM::AddressOfOp>( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
typeDescOp, llvmPtrTy, global.getSymName()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
replaceWithAddrOfOrASCast(rewriter, typeDescOp->getLoc(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
global.getAddrSpace(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
getProgramAddressSpace(rewriter), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
global.getSymName(), llvmPtrTy, typeDescOp); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
return mlir::success(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} else if (auto global = module.lookupSymbol<fir::GlobalOp>(typeDescName)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
rewriter.replaceOpWithNewOp<mlir::LLVM::AddressOfOp>( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
typeDescOp, llvmPtrTy, global.getSymName()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
replaceWithAddrOfOrASCast(rewriter, typeDescOp->getLoc(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
getGlobalAddressSpace(rewriter), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
getProgramAddressSpace(rewriter), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
global.getSymName(), llvmPtrTy, typeDescOp); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
return mlir::success(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
return mlir::failure(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -2992,8 +3065,8 @@ struct GlobalOpConversion : public fir::FIROpConversion<fir::GlobalOp> { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
mlir::SymbolRefAttr comdat; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
llvm::ArrayRef<mlir::NamedAttribute> attrs; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
auto g = rewriter.create<mlir::LLVM::GlobalOp>( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
loc, tyAttr, isConst, linkage, global.getSymName(), initAttr, 0, 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
false, false, comdat, attrs, dbgExprs); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
loc, tyAttr, isConst, linkage, global.getSymName(), initAttr, 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
getGlobalAddressSpace(rewriter), false, false, comdat, attrs, dbgExprs); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (global.getAlignment() && *global.getAlignment() > 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
g.setAlignment(*global.getAlignment()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -346,7 +346,10 @@ unsigned ConvertFIRToLLVMPattern::getAllocaAddressSpace( | |
mlir::Operation *parentOp = rewriter.getInsertionBlock()->getParentOp(); | ||
assert(parentOp != nullptr && | ||
"expected insertion block to have parent operation"); | ||
if (auto module = parentOp->getParentOfType<mlir::ModuleOp>()) | ||
auto module = mlir::isa<mlir::ModuleOp>(parentOp) | ||
? mlir::cast<mlir::ModuleOp>(parentOp) | ||
: parentOp->getParentOfType<mlir::ModuleOp>(); | ||
if (module) | ||
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. This change and the one below is not exactly required for this PR. I guess they are small enough to include in this but could go in a separate PR. 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. Thank you, I would like to keep them in this PR, as the main reason I found this problem is that it does result in an error in this PR stack and they do fit in the overall scope of this particular PR I think in adding/addressing various quirks with the address spaces :-) |
||
if (mlir::Attribute addrSpace = | ||
mlir::DataLayout(module).getAllocaMemorySpace()) | ||
return llvm::cast<mlir::IntegerAttr>(addrSpace).getUInt(); | ||
|
@@ -358,11 +361,29 @@ unsigned ConvertFIRToLLVMPattern::getProgramAddressSpace( | |
mlir::Operation *parentOp = rewriter.getInsertionBlock()->getParentOp(); | ||
assert(parentOp != nullptr && | ||
"expected insertion block to have parent operation"); | ||
if (auto module = parentOp->getParentOfType<mlir::ModuleOp>()) | ||
auto module = mlir::isa<mlir::ModuleOp>(parentOp) | ||
? mlir::cast<mlir::ModuleOp>(parentOp) | ||
: parentOp->getParentOfType<mlir::ModuleOp>(); | ||
if (module) | ||
if (mlir::Attribute addrSpace = | ||
mlir::DataLayout(module).getProgramMemorySpace()) | ||
return llvm::cast<mlir::IntegerAttr>(addrSpace).getUInt(); | ||
return defaultAddressSpace; | ||
} | ||
|
||
unsigned ConvertFIRToLLVMPattern::getGlobalAddressSpace( | ||
mlir::ConversionPatternRewriter &rewriter) const { | ||
mlir::Operation *parentOp = rewriter.getInsertionBlock()->getParentOp(); | ||
assert(parentOp != nullptr && | ||
"expected insertion block to have parent operation"); | ||
auto module = mlir::isa<mlir::ModuleOp>(parentOp) | ||
? mlir::cast<mlir::ModuleOp>(parentOp) | ||
: parentOp->getParentOfType<mlir::ModuleOp>(); | ||
if (module) | ||
if (mlir::Attribute addrSpace = | ||
mlir::DataLayout(module).getGlobalMemorySpace()) | ||
return llvm::cast<mlir::IntegerAttr>(addrSpace).getUInt(); | ||
return defaultAddressSpace; | ||
} | ||
|
||
} // namespace fir |
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.