Skip to content

Commit 6c407ec

Browse files
authored
[XeVM] Attach target pass #422
This PR adds a pass that attaches an xevm target to a GPU module. This enables generating binaries similar to the upstream via gpu-module-to-binary.
1 parent 9dd4e60 commit 6c407ec

File tree

7 files changed

+139
-0
lines changed

7 files changed

+139
-0
lines changed

include/gc/Transforms/Passes.td

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,41 @@ def GpuTilingAndFusion : Pass<"gpu-tiling", "func::FuncOp"> {
150150
"The maximum workgroup size.">
151151
];
152152
}
153+
154+
def GpuXeVMAttachTarget: Pass<"xevm-attach-target", ""> {
155+
let summary = "Attaches a XeVM target attribute to a GPU Module.";
156+
let description = [{
157+
This pass searches for all GPU Modules in the immediate regions and attaches
158+
a XeVM target if the module matches the name specified by the `module` argument.
159+
160+
Example:
161+
```
162+
// File: in.mlir:
163+
gpu.module @nvvm_module_1 {...}
164+
gpu.module @rocdl_module_2 {...}
165+
gpu.module @xevm_module_3 {...}
166+
// mlir-opt --xevm-attach-target="module=xevm.* chip=pvc" in.mlir
167+
gpu.module @nvvm_module_1 {...}
168+
gpu.module @rocdl_module_2 {...}
169+
gpu.module @xevm_module_3 [#xevm.target<chip = "pvc">] {...}
170+
```
171+
}];
172+
let options = [
173+
Option<"moduleMatcher", "module", "std::string",
174+
/*default=*/ [{""}],
175+
"Regex used to identify the modules to attach the target to.">,
176+
Option<"triple", "triple", "std::string",
177+
/*default=*/ "\"spirv64-unknown-unknown\"",
178+
"Target triple.">,
179+
Option<"chip", "chip", "std::string",
180+
/*default=*/"\"pvc\"",
181+
"Target chip.">,
182+
Option<"optLevel", "O", "unsigned",
183+
/*default=*/"2",
184+
"Optimization level.">
185+
];
186+
}
187+
153188
#endif // GC_USE_IMEX
154189

155190
def IterativeTilingAndFusion : Pass<"iterative-tiling-and-fusion",

lib/gc/Target/LLVM/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ gc_add_mlir_dialect_library(MLIRXeVMTarget
66
ADDITIONAL_HEADER_DIRS
77
${MLIR_MAIN_INCLUDE_DIR}/mlir/Dialect/LLVMIR
88
${PROJECT_SOURCE_DIR}/include/gc/Dialect/LLVMIR
9+
10+
LINK_COMPONENTS
11+
SPIRVCodeGen
912

1013
LINK_LIBS PUBLIC
1114
MLIRIR

lib/gc/Transforms/GPU/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ gc_add_mlir_library(GcGpuPasses
1717
GpuToGpuOcl.cpp
1818
LinalgToXeGPU.cpp
1919
Pipeline.cpp
20+
XeVMAttachTarget.cpp
2021

2122
DEPENDS
2223
GraphCompilerPassIncGen
@@ -31,6 +32,7 @@ gc_add_mlir_library(GcGpuPasses
3132
MLIRMathToSPIRV
3233
MLIRControlFlowToSPIRV
3334
MLIRMemRefTransforms
35+
MLIRXeVMToLLVMIRTranslation
3436
GcInterface
3537
GcUtilsIR
3638
${IMEX_LIBS}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//===-- XeVMAttachTarget.cpp - DESC -----------------------------*- C++ -*-===//
2+
//
3+
// This file is licensed under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file implements the `GpuXeVMAttachTarget` pass, attaching `#xevm.target`
10+
// attributes to GPU modules.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#include "gc/Dialect/LLVMIR/XeVMDialect.h"
15+
16+
#include "gc/Target/LLVM/XeVM/Target.h"
17+
#include "gc/Transforms/Passes.h"
18+
19+
#include "mlir/Dialect/GPU/IR/GPUDialect.h"
20+
#include "mlir/Dialect/GPU/Transforms/Passes.h"
21+
#include "mlir/IR/Builders.h"
22+
#include "mlir/Pass/Pass.h"
23+
#include "llvm/Support/Regex.h"
24+
25+
namespace mlir {
26+
namespace gc {
27+
#define GEN_PASS_DEF_GPUXEVMATTACHTARGET
28+
#include "gc/Transforms/Passes.h.inc"
29+
} // namespace gc
30+
} // namespace mlir
31+
32+
using namespace mlir::xevm;
33+
using namespace mlir;
34+
35+
namespace {
36+
struct XeVMAttachTarget
37+
: public gc::impl::GpuXeVMAttachTargetBase<XeVMAttachTarget> {
38+
using Base::Base;
39+
40+
// DictionaryAttr getFlags(OpBuilder &builder) const;
41+
42+
void runOnOperation() override;
43+
44+
void getDependentDialects(DialectRegistry &registry) const override {
45+
registry.insert<xevm::XeVMDialect>();
46+
}
47+
};
48+
} // namespace
49+
50+
void XeVMAttachTarget::runOnOperation() {
51+
OpBuilder builder(&getContext());
52+
auto target = builder.getAttr<XeVMTargetAttr>(optLevel, triple, chip);
53+
llvm::Regex matcher(moduleMatcher);
54+
for (Region &region : getOperation()->getRegions())
55+
for (Block &block : region.getBlocks())
56+
for (auto module : block.getOps<gpu::GPUModuleOp>()) {
57+
// Check if the name of the module matches.
58+
if (!moduleMatcher.empty() && !matcher.match(module.getName()))
59+
continue;
60+
// Create the target array.
61+
SmallVector<Attribute> targets;
62+
if (std::optional<ArrayAttr> attrs = module.getTargets())
63+
targets.append(attrs->getValue().begin(), attrs->getValue().end());
64+
targets.push_back(target);
65+
// Remove any duplicate targets.
66+
targets.erase(llvm::unique(targets), targets.end());
67+
// Update the target attribute array.
68+
module.setTargetsAttr(builder.getArrayAttr(targets));
69+
}
70+
}

src/gc-opt/gc-opt.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,14 @@
2727
#include "gc/Conversion/Passes.h"
2828
#include "mlir/Target/LLVMIR/Dialect/All.h"
2929

30+
#include "gc/Target/LLVM/XeVM/Target.h"
31+
#include "gc/Target/LLVMIR/Dialect/XeVM/XeVMToLLVMIRTranslation.h"
3032
#include "gc/Transforms/Microkernel/MicrokernelPasses.h"
3133
#include "gc/Transforms/Passes.h"
3234
#include "mlir/InitAllDialects.h"
3335
#include "mlir/InitAllExtensions.h"
3436
#include "mlir/InitAllPasses.h"
37+
#include "mlir/Target/LLVMIR/Dialect/All.h"
3538
#include "mlir/Tools/mlir-opt/MlirOptMain.h"
3639

3740
#ifdef GC_USE_IMEX
@@ -75,7 +78,11 @@ int main(int argc, char *argv[]) {
7578
mlir::registerAllDialects(registry);
7679
#ifdef GC_USE_IMEX
7780
registry.insert<::imex::xetile::XeTileDialect, ::imex::gpux::GPUXDialect>();
81+
mlir::registerXeVMDialectTranslation(registry);
82+
mlir::xevm::registerXeVMTargetInterfaceExternalModels(registry);
7883
#endif
84+
mlir::registerAllExtensions(registry);
85+
mlir::registerAllToLLVMIRTranslations(registry);
7986
mlir::cpuruntime::registerConvertCPURuntimeToLLVMInterface(registry);
8087
mlir::registerAllExtensions(registry); // TODO: cleanup
8188
// Adds missing `LLVMTranslationDialectInterface` registration for dialect for
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: gc-opt %s --gpu-to-llvm --convert-gpu-to-llvm-spv --gpu-module-to-binary | FileCheck %s
2+
3+
module attributes {gpu.container_module} {
4+
// CHECK-LABEL:gpu.binary @entry_kernel
5+
// CHECK:[#gpu.object<#xevm.target,
6+
gpu.module @entry_kernel [#xevm.target] {
7+
gpu.func @entry_kernel(%arg0: index) kernel attributes {} {
8+
gpu.return
9+
}
10+
}
11+
}
12+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: gc-opt %s --xevm-attach-target | FileCheck %s
2+
module attributes {gpu.container_module} {
3+
//CHECK:gpu.module @entry_kernel [#xevm.target]
4+
gpu.module @entry_kernel {
5+
gpu.func @entry_kernel(%arg0: index) kernel attributes {} {
6+
gpu.return
7+
}
8+
}
9+
}
10+

0 commit comments

Comments
 (0)