Skip to content

Commit d9df057

Browse files
committed
lower via upstream path
1 parent de64742 commit d9df057

File tree

14 files changed

+207
-56
lines changed

14 files changed

+207
-56
lines changed

include/gc/Conversion/XeVMToLLVM/XeVMToLLVM.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class RewritePatternSet;
1717
class Pass;
1818

1919
#define GEN_PASS_DECL_CONVERTXEVMTOLLVMPASS
20-
#include "mlir/Conversion/Passes.h.inc"
20+
#include "gc/Conversion/Passes.h.inc"
2121

2222
void populateXeVMToLLVMConversionPatterns(RewritePatternSet &patterns);
2323

include/gc/ExecutionEngine/Driver/Driver.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace mlir {
1818
class DialectRegistry;
1919
namespace gc {
2020

21-
const DialectRegistry &initCompilerAndGetDialects();
21+
DialectRegistry &initCompilerAndGetDialects();
2222

2323
// the pointers to XXXMemRefType
2424
using GeneralMemrefPtr = void *;

include/gc/Transforms/Passes.td

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,4 +261,38 @@ def LowerToTileVector : Pass<"lower-to-tile-vector", "func::FuncOp"> {
261261
];
262262
}
263263

264+
def GpuXeVMAttachTarget: Pass<"xevm-attach-target", ""> {
265+
let summary = "Attaches a XeVM target attribute to a GPU Module.";
266+
let description = [{
267+
This pass searches for all GPU Modules in the immediate regions and attaches
268+
a XeVM target if the module matches the name specified by the `module` argument.
269+
270+
Example:
271+
```
272+
// File: in.mlir:
273+
gpu.module @xevm_module_1 {...}
274+
gpu.module @xevm_module_2 {...}
275+
gpu.module @xevm_module_1 {...}
276+
// mlir-opt --xevm-attach-target="module=xevm.* chip=pvc" in.mlir
277+
gpu.module @xevm_module_1 {...}
278+
gpu.module @xevm_module_2 {...}
279+
gpu.module @xevm_module_1 [#xevm.target<chip = "pvc">] {...}
280+
```
281+
}];
282+
let options = [
283+
Option<"moduleMatcher", "module", "std::string",
284+
/*default=*/ [{""}],
285+
"Regex used to identify the modules to attach the target to.">,
286+
Option<"triple", "triple", "std::string",
287+
/*default=*/ "\"spirv64-unknown-unknown\"",
288+
"Target triple.">,
289+
Option<"chip", "chip", "std::string",
290+
/*default=*/"\"pvc\"",
291+
"Target chip.">,
292+
Option<"optLevel", "O", "unsigned",
293+
/*default=*/"2",
294+
"Optimization level.">
295+
];
296+
}
297+
264298
#endif // GC_DIALECT_GC_PASSES

lib/gc/ExecutionEngine/Driver/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ else()
2525
MLIRToLLVMIRTranslationRegistration
2626
)
2727
endif()
28+
get_property(extension_libs GLOBAL PROPERTY MLIR_EXTENSION_LIBS)
2829

2930
set(GC_PASSES GcInterface GcPasses)
3031
if(GC_ENABLE_IMEX)
@@ -38,6 +39,7 @@ gc_add_mlir_library(GcJitWrapper
3839
${MLIR_LINK_COMPONENTS}
3940
${dialect_libs}
4041
${conversion_libs}
42+
${extension_libs}
4143
${GC_PASSES}
4244
GcAnalysis
4345
MLIRXeVMToLLVMIRTranslation

lib/gc/ExecutionEngine/Driver/Driver.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
#include "gc/Dialect/OneDNNGraph/OneDNNGraphDialect.h"
1313
#endif
1414
#include "gc/Conversion/Passes.h"
15+
#include "gc/Target/LLVM/XeVM/Target.h"
1516
#include "gc/Target/LLVMIR/Dialect/XeVM/XeVMToLLVMIRTranslation.h"
1617
#include "gc/Transforms/Passes.h"
1718
#include "mlir/InitAllDialects.h"
19+
#include "mlir/InitAllExtensions.h"
1820
#include "mlir/InitAllPasses.h"
1921
#include "mlir/Pass/PassManager.h"
2022
#include "mlir/Target/LLVMIR/Dialect/All.h"
@@ -34,19 +36,23 @@ static DialectRegistry initDialects() {
3436
registry.insert<mlir::cpuruntime::CPURuntimeDialect>();
3537
mlir::registerAllDialects(registry);
3638
mlir::cpuruntime::registerConvertCPURuntimeToLLVMInterface(registry);
39+
mlir::registerAllExtensions(registry);
40+
// Adds missing `LLVMTranslationDialectInterface` registration for dialect for
41+
// gpu.module op
42+
mlir::registerAllToLLVMIRTranslations(registry);
3743
mlir::registerConvertXeVMToLLVMInterface(registry);
44+
mlir::registerXeVMDialectTranslation(registry);
45+
mlir::xevm::registerXeVMTargetInterfaceExternalModels(registry);
3846
#ifdef GC_HAS_ONEDNN_DIALECT
3947
registry.insert<mlir::onednn_graph::OneDNNGraphDialect>();
4048
#endif
4149
llvm::InitializeNativeTarget();
4250
llvm::InitializeNativeTargetAsmPrinter();
4351
llvm::InitializeNativeTargetAsmParser();
44-
mlir::registerAllToLLVMIRTranslations(registry);
45-
mlir::registerXeVMDialectTranslation(registry);
4652
return registry;
4753
}
4854

49-
const DialectRegistry &initCompilerAndGetDialects() {
55+
DialectRegistry &initCompilerAndGetDialects() {
5056
static DialectRegistry reg = initDialects();
5157
return reg;
5258
}

lib/gc/Target/LLVM/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ gc_add_mlir_dialect_library(MLIRXeVMTarget
77
${MLIR_MAIN_INCLUDE_DIR}/mlir/Dialect/LLVMIR
88
${PROJECT_SOURCE_DIR}/include/gc/Dialect/LLVMIR
99

10+
LINK_COMPONENTS
11+
SPIRVCodeGen
12+
1013
LINK_LIBS PUBLIC
1114
MLIRIR
1215
MLIRExecutionEngineUtils

lib/gc/Transforms/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ gc_add_mlir_library(GcPasses
3030

3131
DEPENDS
3232
GraphCompilerPassIncGen
33-
33+
GCConversionPassIncGen
34+
3435
LINK_LIBS PUBLIC
3536
${mlir_dialect_libs}
3637
${mlir_conversion_libs}

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: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
//===- XeVMAttachTarget.cpp - Attach an XeVM target -----------------------===//
2+
//
3+
// Part of the LLVM Project, 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+
#include <iostream>
25+
26+
namespace mlir {
27+
namespace gc {
28+
#define GEN_PASS_DEF_GPUXEVMATTACHTARGET
29+
#include "gc/Transforms/Passes.h.inc"
30+
} // namespace gc
31+
} // namespace mlir
32+
33+
using namespace mlir::xevm;
34+
using namespace mlir;
35+
36+
namespace {
37+
struct XeVMAttachTarget
38+
: public gc::impl::GpuXeVMAttachTargetBase<XeVMAttachTarget> {
39+
using Base::Base;
40+
41+
DictionaryAttr getFlags(OpBuilder &builder) const;
42+
43+
void runOnOperation() override;
44+
45+
void getDependentDialects(DialectRegistry &registry) const override {
46+
registry.insert<xevm::XeVMDialect>();
47+
}
48+
};
49+
} // namespace
50+
51+
DictionaryAttr XeVMAttachTarget::getFlags(OpBuilder &builder) const {
52+
UnitAttr unitAttr = builder.getUnitAttr();
53+
SmallVector<NamedAttribute, 2> flags;
54+
auto addFlag = [&](StringRef flag) {
55+
flags.push_back(builder.getNamedAttr(flag, unitAttr));
56+
};
57+
if (!flags.empty())
58+
return builder.getDictionaryAttr(flags);
59+
return nullptr;
60+
}
61+
62+
void XeVMAttachTarget::runOnOperation() {
63+
OpBuilder builder(&getContext());
64+
auto target = builder.getAttr<XeVMTargetAttr>(optLevel, triple, chip);
65+
llvm::Regex matcher(moduleMatcher);
66+
for (Region &region : getOperation()->getRegions())
67+
for (Block &block : region.getBlocks())
68+
for (auto module : block.getOps<gpu::GPUModuleOp>()) {
69+
// Check if the name of the module matches.
70+
if (!moduleMatcher.empty() && !matcher.match(module.getName()))
71+
continue;
72+
// Create the target array.
73+
SmallVector<Attribute> targets;
74+
if (std::optional<ArrayAttr> attrs = module.getTargets())
75+
targets.append(attrs->getValue().begin(), attrs->getValue().end());
76+
targets.push_back(target);
77+
// Remove any duplicate targets.
78+
targets.erase(llvm::unique(targets), targets.end());
79+
// Update the target attribute array.
80+
module.setTargetsAttr(builder.getArrayAttr(targets));
81+
}
82+
}

src/gc-cpu-runner/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@ if(GC_DEV_LINK_LLVM_DYLIB)
3232
endif()
3333

3434
gc_add_mlir_tool(gc-cpu-runner gc-cpu-runner.cpp)
35-
target_link_libraries(gc-cpu-runner PRIVATE GcCpuRuntime)
35+
target_link_libraries(gc-cpu-runner PRIVATE GcJitWrapper GcCpuRuntime)
3636
mlir_check_all_link_libraries(gc-cpu-runner)

0 commit comments

Comments
 (0)