Skip to content

Commit ec9f218

Browse files
authored
[mlir][gpu][target] Use promises to verify TargetAttrs IR correctness. (#65787)
This patch employs the updated promise mechanism to enforce Target Attribute IR constraints. Due to this patch, TargetAttributes implementations no longer have to be registered before executing translation to LLVM IR in cases where they are not needed, like when translating `gpu.binary` operations.
1 parent 7b3f6e6 commit ec9f218

File tree

5 files changed

+33
-17
lines changed

5 files changed

+33
-17
lines changed

mlir/include/mlir/Dialect/GPU/IR/CompilationAttrInterfaces.td

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,15 @@ def GPUTargetAttrInterface : AttrInterface<"TargetAttrInterface"> {
4242
];
4343
}
4444

45-
def GPUTargetArrayAttr : TypedArrayAttrBase<GPUTargetAttrInterface,
45+
def GPUTargetAttr :
46+
ConfinedAttr<AnyAttr, [PromisedAttrInterface<GPUTargetAttrInterface>]> {
47+
let description = [{
48+
Generic GPU target attribute. These attributes must implement or promise
49+
the `GPUTargetAttrInterface` interface.
50+
}];
51+
}
52+
53+
def GPUTargetArrayAttr : TypedArrayAttrBase<GPUTargetAttr,
4654
"array of GPU target attributes">;
4755

4856
def GPUNonEmptyTargetArrayAttr :
@@ -51,6 +59,7 @@ def GPUNonEmptyTargetArrayAttr :
5159
//===----------------------------------------------------------------------===//
5260
// GPU offloading translation attribute trait.
5361
//===----------------------------------------------------------------------===//
62+
5463
def OffloadingTranslationAttrTrait :
5564
NativeTrait<"OffloadingTranslationAttrTrait", ""> {
5665
let cppNamespace = "::mlir::gpu";
@@ -65,7 +74,7 @@ def OffloadingTranslationAttr :
6574
ConfinedAttr<AnyAttr, [HasOffloadingTranslationAttrTrait]> {
6675
let description = [{
6776
Generic GPU offloading translation attribute. These attributes must
68-
implement an interface for handling the translation of PU offloading
77+
implement an interface for handling the translation of GPU offloading
6978
operations like `gpu.binary` & `gpu.launch_func`. An example of such
7079
interface is the `OffloadingLLVMTranslationAttrInterface` interface.
7180
}];

mlir/include/mlir/Dialect/GPU/IR/CompilationAttrs.td

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ def GPU_ObjectAttr : GPU_Attr<"Object", "object"> {
3232
#gpu.object<#nvvm.target, "...">
3333
```
3434
}];
35-
let parameters = (ins "TargetAttrInterface":$target, "StringAttr":$object);
35+
let parameters = (ins "Attribute":$target, "StringAttr":$object);
3636
let assemblyFormat = [{`<` $target `,` $object `>`}];
37+
let genVerifyDecl = 1;
3738
}
3839

3940
def GPUObjectArrayAttr :

mlir/include/mlir/Target/LLVMIR/Dialect/All.h

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
#ifndef MLIR_TARGET_LLVMIR_DIALECT_ALL_H
1515
#define MLIR_TARGET_LLVMIR_DIALECT_ALL_H
1616

17-
#include "mlir/Target/LLVM/NVVM/Target.h"
18-
#include "mlir/Target/LLVM/ROCDL/Target.h"
1917
#include "mlir/Target/LLVMIR/Dialect/AMX/AMXToLLVMIRTranslation.h"
2018
#include "mlir/Target/LLVMIR/Dialect/ArmNeon/ArmNeonToLLVMIRTranslation.h"
2119
#include "mlir/Target/LLVMIR/Dialect/ArmSME/ArmSMEToLLVMIRTranslation.h"
@@ -51,13 +49,6 @@ static inline void registerAllToLLVMIRTranslations(DialectRegistry &registry) {
5149

5250
// Extension required for translating GPU offloading Ops.
5351
gpu::registerOffloadingLLVMTranslationInterfaceExternalModels(registry);
54-
55-
// GPU target attribute interfaces are not used during translation, however
56-
// the IR fails to verify if they are not registered due to the promise
57-
// mechanism.
58-
// TODO: remove these.
59-
NVVM::registerNVVMTargetInterfaceExternalModels(registry);
60-
ROCDL::registerROCDLTargetInterfaceExternalModels(registry);
6152
}
6253

6354
/// Registers all the translations to LLVM IR required by GPU passes.
@@ -70,6 +61,9 @@ registerAllGPUToLLVMIRTranslations(DialectRegistry &registry) {
7061
registerLLVMDialectTranslation(registry);
7162
registerNVVMDialectTranslation(registry);
7263
registerROCDLDialectTranslation(registry);
64+
65+
// Extension required for translating GPU offloading Ops.
66+
gpu::registerOffloadingLLVMTranslationInterfaceExternalModels(registry);
7367
}
7468

7569
/// Registers all dialects that can be translated from LLVM IR and the

mlir/lib/Dialect/GPU/IR/GPUDialect.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1954,6 +1954,20 @@ void AllocOp::getCanonicalizationPatterns(RewritePatternSet &results,
19541954
results.add<SimplifyDimOfAllocOp>(context);
19551955
}
19561956

1957+
//===----------------------------------------------------------------------===//
1958+
// GPU object attribute
1959+
//===----------------------------------------------------------------------===//
1960+
1961+
LogicalResult ObjectAttr::verify(function_ref<InFlightDiagnostic()> emitError,
1962+
Attribute target, StringAttr object) {
1963+
if (!target)
1964+
return emitError() << "the target attribute cannot be null";
1965+
if (target.hasPromiseOrImplementsInterface<TargetAttrInterface>())
1966+
return success();
1967+
return emitError() << "the target attribute must implement or promise the "
1968+
"`gpu::TargetAttrInterface`";
1969+
}
1970+
19571971
//===----------------------------------------------------------------------===//
19581972
// GPU select object attribute
19591973
//===----------------------------------------------------------------------===//
@@ -1965,11 +1979,11 @@ gpu::SelectObjectAttr::verify(function_ref<InFlightDiagnostic()> emitError,
19651979
if (target) {
19661980
if (auto intAttr = mlir::dyn_cast<IntegerAttr>(target)) {
19671981
if (intAttr.getInt() < 0) {
1968-
return emitError() << "The object index must be positive.";
1982+
return emitError() << "the object index must be positive";
19691983
}
1970-
} else if (!(::mlir::isa<TargetAttrInterface>(target))) {
1984+
} else if (!target.hasPromiseOrImplementsInterface<TargetAttrInterface>()) {
19711985
return emitError()
1972-
<< "The target attribute must be a GPU Target attribute.";
1986+
<< "the target attribute must be a GPU Target attribute";
19731987
}
19741988
}
19751989
return success();

mlir/lib/Target/LLVMIR/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ add_mlir_translation_library(MLIRToLLVMIRTranslationRegistration
5757
MLIROpenACCToLLVMIRTranslation
5858
MLIROpenMPToLLVMIRTranslation
5959
MLIRROCDLToLLVMIRTranslation
60-
MLIRNVVMTarget
61-
MLIRROCDLTarget
6260
)
6361

6462
add_mlir_translation_library(MLIRTargetLLVMIRImport

0 commit comments

Comments
 (0)