Skip to content

Commit 58827e0

Browse files
committed
Add polygeist NoopOp
Use a noop to preserve structure of gpu kernels through optimizations
1 parent d62661c commit 58827e0

File tree

7 files changed

+324
-58
lines changed

7 files changed

+324
-58
lines changed

include/polygeist/Passes/Passes.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
#include <memory>
77

88
enum PolygeistAlternativesMode { PAM_Static, PAM_PGO_Profile, PAM_PGO_Opt };
9+
enum PolygeistGPUStructureMode {
10+
PGSM_Discard,
11+
PGSM_BlockThreadWrappers,
12+
PGSM_ThreadNoop,
13+
PGSM_BlockThreadNoops
14+
};
915

1016
namespace mlir {
1117
class PatternRewriter;
@@ -25,9 +31,9 @@ std::unique_ptr<Pass> createCPUifyPass(StringRef method = "");
2531
std::unique_ptr<Pass> createBarrierRemovalContinuation();
2632
std::unique_ptr<Pass> detectReductionPass();
2733
std::unique_ptr<Pass> createRemoveTrivialUsePass();
28-
std::unique_ptr<Pass>
29-
createParallelLowerPass(bool wrapParallelOps = false,
30-
bool preserveGPUKernelStructure = false);
34+
std::unique_ptr<Pass> createParallelLowerPass(
35+
bool wrapParallelOps = false,
36+
PolygeistGPUStructureMode gpuKernelStructureMode = PGSM_Discard);
3137
std::unique_ptr<Pass> createCudaRTLowerPass();
3238
std::unique_ptr<Pass>
3339
createConvertPolygeistToLLVMPass(const LowerToLLVMOptions &options,
@@ -36,7 +42,8 @@ std::unique_ptr<Pass> createConvertPolygeistToLLVMPass();
3642
std::unique_ptr<Pass> createForBreakToWhilePass();
3743
std::unique_ptr<Pass>
3844
createConvertParallelToGPUPass1(bool useOriginalThreadNums = false);
39-
std::unique_ptr<Pass> createConvertParallelToGPUPass2(bool emitGPUKernelLaunchBounds = true);
45+
std::unique_ptr<Pass>
46+
createConvertParallelToGPUPass2(bool emitGPUKernelLaunchBounds = true);
4047
std::unique_ptr<Pass> createGpuSerializeToCubinPass(
4148
StringRef triple, StringRef arch, StringRef features, int llvmOptLevel,
4249
int ptxasOptLevel, std::string ptxasPath, std::string libDevicePath,

include/polygeist/PolygeistOps.td

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ include "mlir/IR/SymbolInterfaces.td"
1818
include "mlir/Dialect/LLVMIR/LLVMOpBase.td"
1919
include "mlir/Dialect/LLVMIR/LLVMOpsInterfaces.td"
2020

21+
def NoopOp
22+
: Polygeist_Op<"noop",
23+
[DeclareOpInterfaceMethods<MemoryEffectsOpInterface>]> {
24+
let summary = "Noop for preventing folding or transformations";
25+
let arguments = (ins Variadic<Index>:$blockDims);
26+
let skipDefaultBuilders = 1;
27+
let builders = [
28+
OpBuilder<(ins "ValueRange":$indices)>];
29+
let description = [{}];
30+
}
31+
2132
def CacheLoad
2233
: Polygeist_Op<"cacheload"> {
2334

lib/polygeist/Ops.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,30 @@ using namespace mlir::arith;
4242
llvm::cl::opt<bool> BarrierOpt("barrier-opt", llvm::cl::init(true),
4343
llvm::cl::desc("Optimize barriers"));
4444

45+
//===----------------------------------------------------------------------===//
46+
// NoopOp
47+
//===----------------------------------------------------------------------===//
48+
49+
struct NoopResource : public SideEffects::Resource::Base<NoopResource> {
50+
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(NoopResource)
51+
52+
StringRef getName() final { return "<NoopResource>"; }
53+
};
54+
55+
void NoopOp::build(OpBuilder &builder, OperationState &result,
56+
ValueRange indices) {
57+
result.addOperands(indices);
58+
}
59+
60+
void NoopOp::getEffects(
61+
SmallVectorImpl<MemoryEffects::EffectInstance> &effects) {
62+
// TODO CHECK is it okay to ::get() a new resource every time?
63+
SideEffects::Resource *resource = NoopResource::get();
64+
MemoryEffects::Effect *effect =
65+
MemoryEffects::Effect::get<MemoryEffects::Write>();
66+
effects.emplace_back(effect, resource);
67+
}
68+
4569
//===----------------------------------------------------------------------===//
4670
// GPUErrorOp
4771
//===----------------------------------------------------------------------===//
@@ -652,6 +676,8 @@ static bool mayAlias(Value v, Value v2) {
652676

653677
bool mayAlias(MemoryEffects::EffectInstance a,
654678
MemoryEffects::EffectInstance b) {
679+
if (a.getResource()->getResourceID() != b.getResource()->getResourceID())
680+
return false;
655681
if (Value v2 = b.getValue()) {
656682
return mayAlias(a, v2);
657683
} else if (Value v = a.getValue()) {

0 commit comments

Comments
 (0)