Skip to content

Commit 4186805

Browse files
authored
[Flang][MLIR] Extend DataLayout utilities to have basic GPU Module support (#123149)
As there is now certain areas where we now have the possibility of having either a ModuleOp or GPUModuleOp and both of these modules can have DataLayout's and we may require utilising the DataLayout utilities in these areas I've taken the liberty of trying to extend them for use with both. Those with more knowledge of how they wish the GPUModuleOp's to interact with their parent ModuleOp's DataLayout may have further alterations they wish to make in the future, but for the moment, it'll simply utilise the basic data layout construction which I believe combines parent and child datalayouts from the ModuleOp and GPUModuleOp. If there is no GPUModuleOp DataLayout it should default to the parent ModuleOp. It's worth noting there is some weirdness if you have two module operations defining builtin dialect DataLayout Entries, it appears the combinatorial functionality for DataLayouts doesn't support the merging of these. This behaviour is useful for areas like: https://github.com/llvm/llvm-project/pull/119585/files#diff-19fc4bcb38829d085e25d601d344bbd85bf7ef749ca359e348f4a7c750eae89dR1412 where we have a crossroads between the two different module operations.
1 parent 5b65896 commit 4186805

File tree

10 files changed

+79
-29
lines changed

10 files changed

+79
-29
lines changed

flang/include/flang/Optimizer/Support/DataLayout.h

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@
1818

1919
namespace mlir {
2020
class ModuleOp;
21-
}
21+
namespace gpu {
22+
class GPUModuleOp;
23+
} // namespace gpu
24+
} // namespace mlir
25+
2226
namespace llvm {
2327
class DataLayout;
24-
}
28+
} // namespace llvm
2529

2630
namespace fir::support {
2731
/// Create an mlir::DataLayoutSpecInterface attribute from an llvm::DataLayout
@@ -30,21 +34,30 @@ namespace fir::support {
3034
/// the llvm::DataLayout on the module.
3135
/// These attributes are replaced if they were already set.
3236
void setMLIRDataLayout(mlir::ModuleOp mlirModule, const llvm::DataLayout &dl);
37+
void setMLIRDataLayout(mlir::gpu::GPUModuleOp mlirModule,
38+
const llvm::DataLayout &dl);
3339

3440
/// Create an mlir::DataLayoutSpecInterface from the llvm.data_layout attribute
3541
/// if one is provided. If such attribute is not available, create a default
3642
/// target independent layout when allowDefaultLayout is true. Otherwise do
3743
/// nothing.
3844
void setMLIRDataLayoutFromAttributes(mlir::ModuleOp mlirModule,
3945
bool allowDefaultLayout);
46+
void setMLIRDataLayoutFromAttributes(mlir::gpu::GPUModuleOp mlirModule,
47+
bool allowDefaultLayout);
4048

4149
/// Create mlir::DataLayout from the data layout information on the
4250
/// mlir::Module. Creates the data layout information attributes with
4351
/// setMLIRDataLayoutFromAttributes if the DLTI attribute is not yet set. If no
4452
/// information is present at all and \p allowDefaultLayout is false, returns
4553
/// std::nullopt.
4654
std::optional<mlir::DataLayout>
47-
getOrSetDataLayout(mlir::ModuleOp mlirModule, bool allowDefaultLayout = false);
55+
getOrSetMLIRDataLayout(mlir::ModuleOp mlirModule,
56+
bool allowDefaultLayout = false);
57+
std::optional<mlir::DataLayout>
58+
getOrSetMLIRDataLayout(mlir::gpu::GPUModuleOp mlirModule,
59+
bool allowDefaultLayout = false);
60+
4861
} // namespace fir::support
4962

5063
#endif // FORTRAN_OPTIMIZER_SUPPORT_DATALAYOUT_H

flang/lib/Optimizer/CodeGen/CodeGen.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,7 +1190,7 @@ genCUFAllocDescriptor(mlir::Location loc,
11901190
mlir::ModuleOp mod, fir::BaseBoxType boxTy,
11911191
const fir::LLVMTypeConverter &typeConverter) {
11921192
std::optional<mlir::DataLayout> dl =
1193-
fir::support::getOrSetDataLayout(mod, /*allowDefaultLayout=*/true);
1193+
fir::support::getOrSetMLIRDataLayout(mod, /*allowDefaultLayout=*/true);
11941194
if (!dl)
11951195
mlir::emitError(mod.getLoc(),
11961196
"module operation must carry a data layout attribute "
@@ -3942,7 +3942,7 @@ class FIRToLLVMLowering
39423942
return signalPassFailure();
39433943

39443944
std::optional<mlir::DataLayout> dl =
3945-
fir::support::getOrSetDataLayout(mod, /*allowDefaultLayout=*/true);
3945+
fir::support::getOrSetMLIRDataLayout(mod, /*allowDefaultLayout=*/true);
39463946
if (!dl) {
39473947
mlir::emitError(mod.getLoc(),
39483948
"module operation must carry a data layout attribute "

flang/lib/Optimizer/CodeGen/TargetRewrite.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ class TargetRewrite : public fir::impl::TargetRewritePassBase<TargetRewrite> {
107107
// TargetRewrite will require querying the type storage sizes, if it was
108108
// not set already, create a DataLayoutSpec for the ModuleOp now.
109109
std::optional<mlir::DataLayout> dl =
110-
fir::support::getOrSetDataLayout(mod, /*allowDefaultLayout=*/true);
110+
fir::support::getOrSetMLIRDataLayout(mod, /*allowDefaultLayout=*/true);
111111
if (!dl) {
112112
mlir::emitError(mod.getLoc(),
113113
"module operation must carry a data layout attribute "

flang/lib/Optimizer/Support/DataLayout.cpp

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "flang/Optimizer/Dialect/Support/FIRContext.h"
1111
#include "flang/Optimizer/Support/FatalError.h"
1212
#include "mlir/Dialect/DLTI/DLTI.h"
13+
#include "mlir/Dialect/GPU/IR/GPUDialect.h"
1314
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
1415
#include "mlir/IR/BuiltinOps.h"
1516
#include "mlir/Interfaces/DataLayoutInterfaces.h"
@@ -20,8 +21,9 @@
2021
#include "llvm/Support/TargetSelect.h"
2122
#include "llvm/Target/TargetMachine.h"
2223

23-
void fir::support::setMLIRDataLayout(mlir::ModuleOp mlirModule,
24-
const llvm::DataLayout &dl) {
24+
namespace {
25+
template <typename ModOpTy>
26+
static void setDataLayout(ModOpTy mlirModule, const llvm::DataLayout &dl) {
2527
mlir::MLIRContext *context = mlirModule.getContext();
2628
mlirModule->setAttr(
2729
mlir::LLVM::LLVMDialect::getDataLayoutAttrName(),
@@ -30,12 +32,14 @@ void fir::support::setMLIRDataLayout(mlir::ModuleOp mlirModule,
3032
mlirModule->setAttr(mlir::DLTIDialect::kDataLayoutAttrName, dlSpec);
3133
}
3234

33-
void fir::support::setMLIRDataLayoutFromAttributes(mlir::ModuleOp mlirModule,
34-
bool allowDefaultLayout) {
35+
template <typename ModOpTy>
36+
static void setDataLayoutFromAttributes(ModOpTy mlirModule,
37+
bool allowDefaultLayout) {
3538
if (mlirModule.getDataLayoutSpec())
3639
return; // Already set.
37-
if (auto dataLayoutString = mlirModule->getAttrOfType<mlir::StringAttr>(
38-
mlir::LLVM::LLVMDialect::getDataLayoutAttrName())) {
40+
if (auto dataLayoutString =
41+
mlirModule->template getAttrOfType<mlir::StringAttr>(
42+
mlir::LLVM::LLVMDialect::getDataLayoutAttrName())) {
3943
llvm::DataLayout llvmDataLayout(dataLayoutString);
4044
fir::support::setMLIRDataLayout(mlirModule, llvmDataLayout);
4145
return;
@@ -46,15 +50,48 @@ void fir::support::setMLIRDataLayoutFromAttributes(mlir::ModuleOp mlirModule,
4650
fir::support::setMLIRDataLayout(mlirModule, llvmDataLayout);
4751
}
4852

49-
std::optional<mlir::DataLayout>
50-
fir::support::getOrSetDataLayout(mlir::ModuleOp mlirModule,
51-
bool allowDefaultLayout) {
52-
if (!mlirModule.getDataLayoutSpec()) {
53+
template <typename ModOpTy>
54+
static std::optional<mlir::DataLayout>
55+
getOrSetDataLayout(ModOpTy mlirModule, bool allowDefaultLayout) {
56+
if (!mlirModule.getDataLayoutSpec())
5357
fir::support::setMLIRDataLayoutFromAttributes(mlirModule,
5458
allowDefaultLayout);
55-
if (!mlirModule.getDataLayoutSpec()) {
56-
return std::nullopt;
57-
}
58-
}
59+
if (!mlirModule.getDataLayoutSpec() &&
60+
!mlir::isa<mlir::gpu::GPUModuleOp>(mlirModule))
61+
return std::nullopt;
5962
return mlir::DataLayout(mlirModule);
6063
}
64+
65+
} // namespace
66+
67+
void fir::support::setMLIRDataLayout(mlir::ModuleOp mlirModule,
68+
const llvm::DataLayout &dl) {
69+
setDataLayout(mlirModule, dl);
70+
}
71+
72+
void fir::support::setMLIRDataLayout(mlir::gpu::GPUModuleOp mlirModule,
73+
const llvm::DataLayout &dl) {
74+
setDataLayout(mlirModule, dl);
75+
}
76+
77+
void fir::support::setMLIRDataLayoutFromAttributes(mlir::ModuleOp mlirModule,
78+
bool allowDefaultLayout) {
79+
setDataLayoutFromAttributes(mlirModule, allowDefaultLayout);
80+
}
81+
82+
void fir::support::setMLIRDataLayoutFromAttributes(
83+
mlir::gpu::GPUModuleOp mlirModule, bool allowDefaultLayout) {
84+
setDataLayoutFromAttributes(mlirModule, allowDefaultLayout);
85+
}
86+
87+
std::optional<mlir::DataLayout>
88+
fir::support::getOrSetMLIRDataLayout(mlir::ModuleOp mlirModule,
89+
bool allowDefaultLayout) {
90+
return getOrSetDataLayout(mlirModule, allowDefaultLayout);
91+
}
92+
93+
std::optional<mlir::DataLayout>
94+
fir::support::getOrSetMLIRDataLayout(mlir::gpu::GPUModuleOp mlirModule,
95+
bool allowDefaultLayout) {
96+
return getOrSetDataLayout(mlirModule, allowDefaultLayout);
97+
}

flang/lib/Optimizer/Transforms/AddDebugInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ void AddDebugInfoPass::runOnOperation() {
523523
llvm::StringRef fileName;
524524
std::string filePath;
525525
std::optional<mlir::DataLayout> dl =
526-
fir::support::getOrSetDataLayout(module, /*allowDefaultLayout=*/true);
526+
fir::support::getOrSetMLIRDataLayout(module, /*allowDefaultLayout=*/true);
527527
if (!dl) {
528528
mlir::emitError(module.getLoc(), "Missing data layout attribute in module");
529529
signalPassFailure();

flang/lib/Optimizer/Transforms/CUFAddConstructor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ struct CUFAddConstructor
5757
auto funcTy =
5858
mlir::LLVM::LLVMFunctionType::get(voidTy, {}, /*isVarArg=*/false);
5959
std::optional<mlir::DataLayout> dl =
60-
fir::support::getOrSetDataLayout(mod, /*allowDefaultLayout=*/false);
60+
fir::support::getOrSetMLIRDataLayout(mod, /*allowDefaultLayout=*/false);
6161
if (!dl) {
6262
mlir::emitError(mod.getLoc(),
6363
"data layout attribute is required to perform " +

flang/lib/Optimizer/Transforms/CUFGPUToLLVMConversion.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,8 @@ class CUFGPUToLLVMConversion
188188
if (!module)
189189
return signalPassFailure();
190190

191-
std::optional<mlir::DataLayout> dl =
192-
fir::support::getOrSetDataLayout(module, /*allowDefaultLayout=*/false);
191+
std::optional<mlir::DataLayout> dl = fir::support::getOrSetMLIRDataLayout(
192+
module, /*allowDefaultLayout=*/false);
193193
fir::LLVMTypeConverter typeConverter(module, /*applyTBAA=*/false,
194194
/*forceUnifiedTBAATree=*/false, *dl);
195195
cuf::populateCUFGPUToLLVMConversionPatterns(typeConverter, patterns);

flang/lib/Optimizer/Transforms/CUFOpConversion.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -919,8 +919,8 @@ class CUFOpConversion : public fir::impl::CUFOpConversionBase<CUFOpConversion> {
919919
return signalPassFailure();
920920
mlir::SymbolTable symtab(module);
921921

922-
std::optional<mlir::DataLayout> dl =
923-
fir::support::getOrSetDataLayout(module, /*allowDefaultLayout=*/false);
922+
std::optional<mlir::DataLayout> dl = fir::support::getOrSetMLIRDataLayout(
923+
module, /*allowDefaultLayout=*/false);
924924
fir::LLVMTypeConverter typeConverter(module, /*applyTBAA=*/false,
925925
/*forceUnifiedTBAATree=*/false, *dl);
926926
target.addLegalDialect<fir::FIROpsDialect, mlir::arith::ArithDialect,

flang/lib/Optimizer/Transforms/LoopVersioning.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,8 @@ void LoopVersioningPass::runOnOperation() {
312312
mlir::ModuleOp module = func->getParentOfType<mlir::ModuleOp>();
313313
fir::KindMapping kindMap = fir::getKindMapping(module);
314314
mlir::SmallVector<ArgInfo, 4> argsOfInterest;
315-
std::optional<mlir::DataLayout> dl =
316-
fir::support::getOrSetDataLayout(module, /*allowDefaultLayout=*/false);
315+
std::optional<mlir::DataLayout> dl = fir::support::getOrSetMLIRDataLayout(
316+
module, /*allowDefaultLayout=*/false);
317317
if (!dl)
318318
mlir::emitError(module.getLoc(),
319319
"data layout attribute is required to perform " DEBUG_TYPE

flang/test/lib/OpenACC/TestOpenACCInterfaces.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ struct TestFIROpenACCInterfaces
2828
void runOnOperation() override {
2929
mlir::ModuleOp mod = getOperation();
3030
auto datalayout =
31-
fir::support::getOrSetDataLayout(mod, /*allowDefaultLayout=*/true);
31+
fir::support::getOrSetMLIRDataLayout(mod, /*allowDefaultLayout=*/true);
3232
mlir::OpBuilder builder(mod);
3333
getOperation().walk([&](Operation *op) {
3434
if (isa<ACC_DATA_ENTRY_OPS>(op)) {

0 commit comments

Comments
 (0)