Skip to content

Commit f1331d3

Browse files
committed
[Nvidia][TritonGPU] Gate RLC phases via compiler.py env switches
1 parent 646b4be commit f1331d3

5 files changed

Lines changed: 56 additions & 28 deletions

File tree

CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ endif()
3535
if(NOT FLAGTREE_BACKEND)
3636
add_definitions(-D__NVIDIA__)
3737
add_definitions(-D__AMD__)
38-
add_definitions(-D__FLAGTREE_RLC_ENHANCE__)
3938
elseif(FLAGTREE_BACKEND STREQUAL "iluvatar")
4039
add_definitions(-D__ILUVATAR__)
4140
set(FLAGTREE_TLE OFF)

include/triton/Dialect/TritonGPU/Transforms/Passes.td

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,23 @@ def TritonGPURemoveLayoutConversions : Pass<"tritongpu-remove-layout-conversions
273273
let dependentDialects = ["mlir::triton::gpu::TritonGPUDialect",
274274
"mlir::triton::TritonDialect"];
275275

276+
let options = [
277+
Option<"enableCostBasedResolution", "enable-cost-based-resolution",
278+
"bool", /*default=*/"false",
279+
"Use cost-based conflict resolution instead of heuristic rules">,
280+
Option<"enableBackwardPropagation", "enable-backward-propagation",
281+
"bool", /*default=*/"false",
282+
"Enable backward layout propagation from convert_layout ops">,
283+
Option<"enableSmallComponentSolving", "enable-small-component-solving",
284+
"bool", /*default=*/"false",
285+
"Enable small local layout component solving">,
286+
Option<"enableStoreLayoutRematerialization",
287+
"enable-store-layout-rematerialization",
288+
"bool", /*default=*/"false",
289+
"Drop store/atomic writeback convert_layout ops by rematerializing "
290+
"their address/mask chains in the value's layout">
291+
];
292+
276293
}
277294

278295
def TritonGPUOptimizeThreadLocality : Pass<"tritongpu-optimize-thread-locality", "mlir::ModuleOp"> {

lib/Dialect/TritonGPU/Transforms/RemoveLayoutConversions.cpp

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,6 @@ namespace mlir::triton::gpu {
4747
#define DBGS() (llvm::dbgs() << "[" DEBUG_TYPE "]: ")
4848
#define LDBG(X) LLVM_DEBUG(DBGS() << X << "\n")
4949

50-
// Master build-time switch for the enhancement phases. The NVIDIA backend
51-
// defines __FLAGTREE_RLC_ENHANCE__ from CMake (on by default); every other
52-
// backend leaves it undefined, keeping the pass at its original behavior.
53-
#ifdef __FLAGTREE_RLC_ENHANCE__
54-
static constexpr bool kFlagtreeRlcEnhance = true;
55-
#else
56-
static constexpr bool kFlagtreeRlcEnhance = false;
57-
#endif
58-
59-
// Per-phase switches, on by default; each is AND-ed with the master switch at
60-
// its use site. Flip one to false here to disable that single phase.
61-
static constexpr bool kEnableCostBasedResolution = true;
62-
static constexpr bool kEnableBackwardPropagation = true;
63-
static constexpr bool kEnableSmallComponentSolving = true;
64-
static constexpr bool kEnableStoreLayoutRematerialization = true;
65-
6650
namespace {
6751

6852
#ifdef __TLE__
@@ -4489,10 +4473,11 @@ class TritonGPURemoveLayoutConversionsPass
44894473
MLIRContext *context = &getContext();
44904474
ModuleOp m = getOperation();
44914475

4492-
bool costBased = kFlagtreeRlcEnhance && kEnableCostBasedResolution;
4493-
bool backwardProp = kFlagtreeRlcEnhance && kEnableBackwardPropagation;
4494-
bool smallComponentSolving =
4495-
kFlagtreeRlcEnhance && kEnableSmallComponentSolving;
4476+
bool costBased = enableCostBasedResolution;
4477+
bool backwardProp = enableBackwardPropagation;
4478+
bool smallComponentSolving = enableSmallComponentSolving;
4479+
bool storeLayoutRemat = enableStoreLayoutRematerialization;
4480+
44964481
if (!costBased) {
44974482
backwardProp = false;
44984483
smallComponentSolving = false;
@@ -4527,8 +4512,6 @@ class TritonGPURemoveLayoutConversionsPass
45274512

45284513
cleanupConvertOps();
45294514

4530-
bool storeLayoutRemat =
4531-
kFlagtreeRlcEnhance && kEnableStoreLayoutRematerialization;
45324515
bool changed = false;
45334516
do {
45344517
changed = false;

python/src/passes.cc

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,18 @@ void init_triton_passes_ttgpuir(py::module &&m) {
7676
ADD_PASS_OPTION_WRAPPER_1("add_f32_dot_tc", createTritonGPUF32DotTC, bool);
7777
ADD_PASS_OPTION_WRAPPER_1("add_optimize_dot_operands",
7878
createTritonGPUOptimizeDotOperands, bool);
79-
ADD_PASS_WRAPPER_0("add_remove_layout_conversions",
80-
createTritonGPURemoveLayoutConversions);
79+
m.def(
80+
"add_remove_layout_conversions",
81+
[](mlir::PassManager &pm, bool costBased, bool backwardProp,
82+
bool smallComponentSolving, bool storeLayoutRemat) {
83+
pm.addPass(createTritonGPURemoveLayoutConversions(
84+
{costBased, backwardProp, smallComponentSolving,
85+
storeLayoutRemat}));
86+
},
87+
py::arg("pm"), py::arg("enable_cost_based_resolution") = false,
88+
py::arg("enable_backward_propagation") = false,
89+
py::arg("enable_small_component_solving") = false,
90+
py::arg("enable_store_layout_rematerialization") = false);
8191
ADD_PASS_WRAPPER_0("add_reduce_data_duplication",
8292
createTritonGPUReduceDataDuplication);
8393
ADD_PASS_WRAPPER_0("add_allocate_warp_groups",

third_party/nvidia/backend/compiler.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,25 @@ def get_ptxas(arch: int) -> knobs.NvidiaTool:
3737
return knobs.nvidia.ptxas_blackwell if arch >= 100 else knobs.nvidia.ptxas
3838

3939

40+
def _env_bool(name: str, default: bool) -> bool:
41+
value = os.environ.get(name)
42+
if value is None:
43+
return default
44+
return value.strip().lower() not in {"0", "false", "off", "no"}
45+
46+
47+
# flagtree: enable RemoveLayoutConversions phases for NVIDIA (env-configurable).
48+
def _add_remove_layout_conversions(pm):
49+
master = _env_bool("FLAGTREE_RLC_ENHANCE", True)
50+
passes.ttgpuir.add_remove_layout_conversions(
51+
pm,
52+
master and _env_bool("FLAGTREE_RLC_COST_BASED_RESOLUTION", True),
53+
master and _env_bool("FLAGTREE_RLC_BACKWARD_PROPAGATION", True),
54+
master and _env_bool("FLAGTREE_RLC_SMALL_COMPONENT_SOLVING", True),
55+
master and _env_bool("FLAGTREE_RLC_STORE_LAYOUT_REMATERIALIZATION", True),
56+
)
57+
58+
4059
@functools.lru_cache()
4160
def get_ptxas_version(arch: int = 80):
4261
mock_ver = knobs.nvidia.mock_ptx_version
@@ -299,7 +318,7 @@ def make_ttgir(mod, metadata, opt, capability):
299318
passes.ttgpuir.add_f32_dot_tc(pm, emuTF32)
300319
# TODO(Qingyi): Move PlanCTAPass to the front of CoalescePass
301320
nvidia.passes.ttnvgpuir.add_plan_cta(pm)
302-
passes.ttgpuir.add_remove_layout_conversions(pm)
321+
_add_remove_layout_conversions(pm)
303322
passes.ttgpuir.add_optimize_thread_locality(pm)
304323
tle.passes.add_early_assign_memory_space(pm)
305324
# begin flagtree tle
@@ -310,7 +329,7 @@ def make_ttgir(mod, metadata, opt, capability):
310329
# end flagtree tle
311330
passes.ttgpuir.add_accelerate_matmul(pm)
312331
tle.passes.add_lower_wgmma(pm)
313-
passes.ttgpuir.add_remove_layout_conversions(pm)
332+
_add_remove_layout_conversions(pm)
314333
passes.ttgpuir.add_optimize_dot_operands(pm, capability >= 80)
315334
tle.passes.add_promote_local_store_staging(pm)
316335
nvidia.passes.ttnvgpuir.add_optimize_descriptor_encoding(pm)
@@ -361,7 +380,7 @@ def make_ttgir(mod, metadata, opt, capability):
361380
tle.passes.add_lower_tma_copy(pm)
362381
tle.passes.add_schedule_tma_store_sync(pm)
363382
nvidia.passes.ttnvgpuir.add_tma_lowering(pm)
364-
passes.ttgpuir.add_remove_layout_conversions(pm)
383+
_add_remove_layout_conversions(pm)
365384
nvidia.passes.ttnvgpuir.add_interleave_tmem(pm)
366385
passes.ttgpuir.add_reduce_data_duplication(pm)
367386
passes.ttgpuir.add_reorder_instructions(pm)

0 commit comments

Comments
 (0)