Skip to content

Commit 747088d

Browse files
authored
Generate spirv::BuiltIn::GlobalInvocationId (#170)
1 parent 3509f46 commit 747088d

File tree

5 files changed

+95
-8
lines changed

5 files changed

+95
-8
lines changed

mlir/include/mlir-extensions/dialect/plier_util/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ set(dialect_namespace plier_util)
44
set(LLVM_TARGET_DEFINITIONS ${dialect}.td)
55
mlir_tablegen(${dialect}Enums.h.inc -gen-enum-decls)
66
mlir_tablegen(${dialect}Enums.cpp.inc -gen-enum-defs)
7+
mlir_tablegen(${dialect}Attributes.h.inc -gen-attrdef-decls -attrdefs-dialect=plier_util)
8+
mlir_tablegen(${dialect}Attributes.cpp.inc -gen-attrdef-defs -attrdefs-dialect=plier_util)
79
mlir_tablegen(${dialect}.h.inc -gen-op-decls)
810
mlir_tablegen(${dialect}.cpp.inc -gen-op-defs)
911
mlir_tablegen(${dialect}Dialect.h.inc -gen-dialect-decls -dialect=${dialect_namespace})

mlir/include/mlir-extensions/dialect/plier_util/PlierUtilOps.td

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
include "mlir/IR/OpBase.td"
1919
include "mlir/Dialect/GPU/GPUBase.td"
20+
include "mlir/IR/EnumAttr.td"
2021
include "mlir/Interfaces/ControlFlowInterfaces.td"
2122
include "mlir/Interfaces/InferTypeOpInterface.td"
2223
include "mlir/Interfaces/LoopLikeInterface.td"
@@ -274,4 +275,26 @@ def ReleaseContextOp : PlierUtil_Op<"release_context"> {
274275
let arguments = (ins PlierUtil_OpaqueType:$context);
275276
}
276277

278+
def GPUEx_Dimension : I32EnumAttr<"GpuDimension",
279+
"a dimension, either 'x', 'y', or 'z'",
280+
[
281+
I32EnumAttrCase<"x", 0>,
282+
I32EnumAttrCase<"y", 1>,
283+
I32EnumAttrCase<"z", 2>
284+
]>{
285+
let genSpecializedAttr = 0;
286+
let cppNamespace = "::plier";
287+
}
288+
def GPUEx_DimensionAttr : EnumAttr<PlierUtil_Dialect, GPUEx_Dimension, "dim">;
289+
290+
class GPU_IndexOp<string mnemonic, list<Trait> traits = []> :
291+
PlierUtil_Op<mnemonic, !listconcat(traits, [NoSideEffect])>,
292+
Arguments<(ins GPUEx_DimensionAttr:$dimension)>, Results<(outs Index)> {
293+
let assemblyFormat = "$dimension attr-dict";
294+
}
295+
296+
def GPU_GlobalIdOp : GPU_IndexOp<"gpu_global_id"> {
297+
let hasCanonicalizer = 1;
298+
}
299+
277300
#endif // PLIER_UTIL_OPS

mlir/include/mlir-extensions/dialect/plier_util/dialect.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@
2727
#include <mlir/Dialect/GPU/GPUDialect.h>
2828

2929
#include "mlir-extensions/dialect/plier_util/PlierUtilOpsDialect.h.inc"
30-
//#include "mlir-extensions/dialect/plier_util/PlierUtilOpsEnums.h.inc"
30+
#include "mlir-extensions/dialect/plier_util/PlierUtilOpsEnums.h.inc"
31+
32+
#define GET_ATTRDEF_CLASSES
33+
#include "mlir-extensions/dialect/plier_util/PlierUtilOpsAttributes.h.inc"
34+
3135
#define GET_OP_CLASSES
3236
#include "mlir-extensions/dialect/plier_util/PlierUtilOps.h.inc"
3337

mlir/lib/dialect/plier_util/dialect.cpp

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,14 @@ void PlierUtilDialect::initialize() {
5656
#define GET_OP_LIST
5757
#include "mlir-extensions/dialect/plier_util/PlierUtilOps.cpp.inc"
5858
>();
59+
5960
addTypes<OpaqueType>();
6061
addInterfaces<PlierUtilInlinerInterface>();
62+
63+
addAttributes<
64+
#define GET_ATTRDEF_LIST
65+
#include "mlir-extensions/dialect/plier_util/PlierUtilOpsAttributes.cpp.inc"
66+
>();
6167
}
6268

6369
mlir::Type PlierUtilDialect::parseType(mlir::DialectAsmParser &parser) const {
@@ -1508,11 +1514,64 @@ void TakeContextOp::build(mlir::OpBuilder &b, mlir::OperationState &result,
15081514
build(b, result, allTypes, initFunc, releaseFunc);
15091515
}
15101516

1517+
namespace {
1518+
struct GenGlobalId : public mlir::OpRewritePattern<mlir::arith::AddIOp> {
1519+
using OpRewritePattern::OpRewritePattern;
1520+
1521+
mlir::LogicalResult
1522+
matchAndRewrite(mlir::arith::AddIOp op,
1523+
mlir::PatternRewriter &rewriter) const override {
1524+
1525+
auto getArg = [](auto op, bool rev) -> mlir::Value {
1526+
return rev ? op.getLhs() : op.getRhs();
1527+
};
1528+
1529+
mlir::gpu::Dimension dim;
1530+
mlir::arith::MulIOp other;
1531+
for (auto rev : {false, true}) {
1532+
auto arg1 = getArg(op, rev);
1533+
auto arg2 = getArg(op, !rev);
1534+
if (auto tid = arg1.getDefiningOp<mlir::gpu::ThreadIdOp>()) {
1535+
dim = tid.dimension();
1536+
other = arg2.getDefiningOp<mlir::arith::MulIOp>();
1537+
break;
1538+
}
1539+
}
1540+
1541+
if (!other)
1542+
return mlir::failure();
1543+
1544+
for (auto rev : {false, true}) {
1545+
auto arg1 = getArg(other, rev).getDefiningOp<mlir::gpu::BlockIdOp>();
1546+
auto arg2 = getArg(other, !rev).getDefiningOp<mlir::gpu::BlockDimOp>();
1547+
if (arg1 && arg2) {
1548+
if (arg1.dimension() != dim || arg2.dimension() != dim)
1549+
return mlir::failure();
1550+
1551+
rewriter.replaceOpWithNewOp<plier::GlobalIdOp>(
1552+
op, static_cast<plier::GpuDimension>(dim));
1553+
return mlir::success();
1554+
}
1555+
}
1556+
1557+
return mlir::failure();
1558+
}
1559+
};
1560+
} // namespace
1561+
1562+
void GlobalIdOp::getCanonicalizationPatterns(::mlir::RewritePatternSet &results,
1563+
::mlir::MLIRContext *context) {
1564+
results.insert<GenGlobalId>(context);
1565+
}
1566+
15111567
} // namespace plier
15121568

15131569
#include "mlir-extensions/dialect/plier_util/PlierUtilOpsDialect.cpp.inc"
15141570

15151571
#define GET_OP_CLASSES
15161572
#include "mlir-extensions/dialect/plier_util/PlierUtilOps.cpp.inc"
15171573

1518-
//#include "mlir-extensions/dialect/plier_util/PlierUtilOpsEnums.cpp.inc"
1574+
#define GET_ATTRDEF_CLASSES
1575+
#include "mlir-extensions/dialect/plier_util/PlierUtilOpsAttributes.cpp.inc"
1576+
1577+
#include "mlir-extensions/dialect/plier_util/PlierUtilOpsEnums.cpp.inc"

numba_dpcomp/numba_dpcomp/mlir_compiler/lib/pipelines/lower_to_gpu.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -870,10 +870,9 @@ struct AbiAttrsPass
870870
void runOnOperation() override {
871871
auto gpuModule = getOperation();
872872
auto *context = &getContext();
873-
auto attrName = mlir::spirv::getEntryPointABIAttrName();
874-
// TODO: Check if block size is const and set appropriate.
875-
const int32_t sizes[] = {0, 0, 0};
876-
auto abi = mlir::spirv::getEntryPointABIAttr(sizes, context);
873+
auto attrName =
874+
mlir::StringAttr::get(context, mlir::spirv::getEntryPointABIAttrName());
875+
auto abi = mlir::spirv::getEntryPointABIAttr(llvm::None, context);
877876
for (auto gpuFunc : gpuModule.getOps<mlir::gpu::GPUFuncOp>()) {
878877
if (!mlir::gpu::GPUDialect::isKernel(gpuFunc) ||
879878
gpuFunc->getAttr(attrName))
@@ -1295,7 +1294,7 @@ struct GPUToSpirvPass
12951294
context);
12961295

12971296
patterns.insert<LaunchConfigConversion<
1298-
mlir::gpu::BlockDimOp, mlir::spirv::BuiltIn::WorkgroupSize>>(
1297+
plier::GlobalIdOp, mlir::spirv::BuiltIn::GlobalInvocationId>>(
12991298
typeConverter, context);
13001299

13011300
if (failed(
@@ -3129,7 +3128,7 @@ static void populateLowerToGPUPipelineLow(mlir::OpPassManager &pm) {
31293128
commonOptPasses(funcPM);
31303129
funcPM.addPass(std::make_unique<KernelMemrefOpsMovementPass>());
31313130
funcPM.addPass(std::make_unique<GpuLaunchSinkOpsPass>());
3132-
// funcPM.addPass(std::make_unique<SinkGpuDimsPass>());
3131+
funcPM.addPass(std::make_unique<SinkGpuDimsPass>());
31333132
pm.addPass(mlir::createGpuKernelOutliningPass());
31343133
pm.addPass(mlir::createSymbolDCEPass());
31353134

0 commit comments

Comments
 (0)