Skip to content

[mlir][gpu][target] Use promises to verify TargetAttrs IR correctness. #65787

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

Merged
merged 1 commit into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions mlir/include/mlir/Dialect/GPU/IR/CompilationAttrInterfaces.td
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,15 @@ def GPUTargetAttrInterface : AttrInterface<"TargetAttrInterface"> {
];
}

def GPUTargetArrayAttr : TypedArrayAttrBase<GPUTargetAttrInterface,
def GPUTargetAttr :
ConfinedAttr<AnyAttr, [PromisedAttrInterface<GPUTargetAttrInterface>]> {
let description = [{
Generic GPU target attribute. These attributes must implement or promise
the `GPUTargetAttrInterface` interface.
}];
}

def GPUTargetArrayAttr : TypedArrayAttrBase<GPUTargetAttr,
"array of GPU target attributes">;

def GPUNonEmptyTargetArrayAttr :
Expand All @@ -51,6 +59,7 @@ def GPUNonEmptyTargetArrayAttr :
//===----------------------------------------------------------------------===//
// GPU offloading translation attribute trait.
//===----------------------------------------------------------------------===//

def OffloadingTranslationAttrTrait :
NativeTrait<"OffloadingTranslationAttrTrait", ""> {
let cppNamespace = "::mlir::gpu";
Expand All @@ -65,7 +74,7 @@ def OffloadingTranslationAttr :
ConfinedAttr<AnyAttr, [HasOffloadingTranslationAttrTrait]> {
let description = [{
Generic GPU offloading translation attribute. These attributes must
implement an interface for handling the translation of PU offloading
implement an interface for handling the translation of GPU offloading
operations like `gpu.binary` & `gpu.launch_func`. An example of such
interface is the `OffloadingLLVMTranslationAttrInterface` interface.
}];
Expand Down
3 changes: 2 additions & 1 deletion mlir/include/mlir/Dialect/GPU/IR/CompilationAttrs.td
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ def GPU_ObjectAttr : GPU_Attr<"Object", "object"> {
#gpu.object<#nvvm.target, "...">
```
}];
let parameters = (ins "TargetAttrInterface":$target, "StringAttr":$object);
let parameters = (ins "Attribute":$target, "StringAttr":$object);
let assemblyFormat = [{`<` $target `,` $object `>`}];
let genVerifyDecl = 1;
}

def GPUObjectArrayAttr :
Expand Down
12 changes: 3 additions & 9 deletions mlir/include/mlir/Target/LLVMIR/Dialect/All.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
#ifndef MLIR_TARGET_LLVMIR_DIALECT_ALL_H
#define MLIR_TARGET_LLVMIR_DIALECT_ALL_H

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

// Extension required for translating GPU offloading Ops.
gpu::registerOffloadingLLVMTranslationInterfaceExternalModels(registry);

// GPU target attribute interfaces are not used during translation, however
// the IR fails to verify if they are not registered due to the promise
// mechanism.
// TODO: remove these.
NVVM::registerNVVMTargetInterfaceExternalModels(registry);
ROCDL::registerROCDLTargetInterfaceExternalModels(registry);
}

/// Registers all the translations to LLVM IR required by GPU passes.
Expand All @@ -70,6 +61,9 @@ registerAllGPUToLLVMIRTranslations(DialectRegistry &registry) {
registerLLVMDialectTranslation(registry);
registerNVVMDialectTranslation(registry);
registerROCDLDialectTranslation(registry);

// Extension required for translating GPU offloading Ops.
gpu::registerOffloadingLLVMTranslationInterfaceExternalModels(registry);
}

/// Registers all dialects that can be translated from LLVM IR and the
Expand Down
20 changes: 17 additions & 3 deletions mlir/lib/Dialect/GPU/IR/GPUDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1954,6 +1954,20 @@ void AllocOp::getCanonicalizationPatterns(RewritePatternSet &results,
results.add<SimplifyDimOfAllocOp>(context);
}

//===----------------------------------------------------------------------===//
// GPU object attribute
//===----------------------------------------------------------------------===//

LogicalResult ObjectAttr::verify(function_ref<InFlightDiagnostic()> emitError,
Attribute target, StringAttr object) {
if (!target)
return emitError() << "the target attribute cannot be null";
if (target.hasPromiseOrImplementsInterface<TargetAttrInterface>())
return success();
return emitError() << "the target attribute must implement or promise the "
"`gpu::TargetAttrInterface`";
}

//===----------------------------------------------------------------------===//
// GPU select object attribute
//===----------------------------------------------------------------------===//
Expand All @@ -1965,11 +1979,11 @@ gpu::SelectObjectAttr::verify(function_ref<InFlightDiagnostic()> emitError,
if (target) {
if (auto intAttr = mlir::dyn_cast<IntegerAttr>(target)) {
if (intAttr.getInt() < 0) {
return emitError() << "The object index must be positive.";
return emitError() << "the object index must be positive";
}
} else if (!(::mlir::isa<TargetAttrInterface>(target))) {
} else if (!target.hasPromiseOrImplementsInterface<TargetAttrInterface>()) {
return emitError()
<< "The target attribute must be a GPU Target attribute.";
<< "the target attribute must be a GPU Target attribute";
}
}
return success();
Expand Down
2 changes: 0 additions & 2 deletions mlir/lib/Target/LLVMIR/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ add_mlir_translation_library(MLIRToLLVMIRTranslationRegistration
MLIROpenACCToLLVMIRTranslation
MLIROpenMPToLLVMIRTranslation
MLIRROCDLToLLVMIRTranslation
MLIRNVVMTarget
MLIRROCDLTarget
)

add_mlir_translation_library(MLIRTargetLLVMIRImport
Expand Down