Skip to content

Commit 93386c2

Browse files
[mlir] Don't rely on values of MLIR_(CURDA|ROCM)_CONVERSIONS_ENABLED.
This PR changes the `#if XXX == 1` sections for these macros to `#ifdef` sections such that the actual values that the macros are defined with do not matter. This scheme is used for most (but not all) such macros in the MLIR code base and the fact that these two macros did not follow it has lead to bugs downstream (which assumed that `#ifdef` was being used and defined them to a value != 1).
1 parent b2ebd8b commit 93386c2

File tree

8 files changed

+18
-18
lines changed

8 files changed

+18
-18
lines changed

mlir/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,20 +108,20 @@ endif()
108108
# is available
109109
if ("NVPTX" IN_LIST LLVM_TARGETS_TO_BUILD)
110110
set(MLIR_ENABLE_CUDA_CONVERSIONS 1)
111+
# TODO: we should use a config.h file like LLVM does
112+
add_definitions(-DMLIR_CUDA_CONVERSIONS_ENABLED)
111113
else()
112114
set(MLIR_ENABLE_CUDA_CONVERSIONS 0)
113115
endif()
114-
# TODO: we should use a config.h file like LLVM does
115-
add_definitions(-DMLIR_CUDA_CONVERSIONS_ENABLED=${MLIR_ENABLE_CUDA_CONVERSIONS})
116116

117117
# Build the ROCm conversions and run according tests if the AMDGPU backend
118118
# is available.
119119
if ("AMDGPU" IN_LIST LLVM_TARGETS_TO_BUILD)
120120
set(MLIR_ENABLE_ROCM_CONVERSIONS 1)
121+
add_definitions(-DMLIR_ROCM_CONVERSIONS_ENABLED)
121122
else()
122123
set(MLIR_ENABLE_ROCM_CONVERSIONS 0)
123124
endif()
124-
add_definitions(-DMLIR_ROCM_CONVERSIONS_ENABLED=${MLIR_ENABLE_ROCM_CONVERSIONS})
125125

126126
set(MLIR_ENABLE_CUDA_RUNNER 0 CACHE BOOL "Enable building the mlir CUDA runner")
127127
set(MLIR_ENABLE_ROCM_RUNNER 0 CACHE BOOL "Enable building the mlir ROCm runner")

mlir/include/mlir/InitAllPasses.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ inline void registerAllPasses() {
9696
bufferization::registerBufferizationPipelines();
9797
sparse_tensor::registerSparseTensorPipelines();
9898
tosa::registerTosaToLinalgPipelines();
99-
#if MLIR_CUDA_CONVERSIONS_ENABLED
99+
#ifdef MLIR_CUDA_CONVERSIONS_ENABLED
100100
gpu::registerGPUToNVVMPipeline();
101101
#endif
102102
}

mlir/lib/Dialect/GPU/Pipelines/GPUToNVVMPipeline.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
using namespace mlir;
4040

41-
#if MLIR_CUDA_CONVERSIONS_ENABLED
41+
#ifdef MLIR_CUDA_CONVERSIONS_ENABLED
4242
namespace {
4343

4444
//===----------------------------------------------------------------------===//

mlir/lib/Dialect/GPU/Transforms/ModuleToBinary.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ void GpuModuleToBinaryPass::getDependentDialects(
4848
// Register all GPU related translations.
4949
registry.insert<gpu::GPUDialect>();
5050
registry.insert<LLVM::LLVMDialect>();
51-
#if MLIR_CUDA_CONVERSIONS_ENABLED == 1
51+
#ifdef MLIR_CUDA_CONVERSIONS_ENABLED
5252
registry.insert<NVVM::NVVMDialect>();
5353
#endif
54-
#if MLIR_ROCM_CONVERSIONS_ENABLED == 1
54+
#ifdef MLIR_ROCM_CONVERSIONS_ENABLED
5555
registry.insert<ROCDL::ROCDLDialect>();
5656
#endif
5757
registry.insert<spirv::SPIRVDialect>();

mlir/lib/Target/LLVM/NVVM/Target.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ SerializeGPUModuleBase::loadBitcodeFiles(llvm::Module &module) {
156156
return std::move(bcFiles);
157157
}
158158

159-
#if MLIR_CUDA_CONVERSIONS_ENABLED == 1
159+
#ifdef MLIR_CUDA_CONVERSIONS_ENABLED
160160
namespace {
161161
class NVPTXSerializer : public SerializeGPUModuleBase {
162162
public:
@@ -555,14 +555,14 @@ NVPTXSerializer::moduleToObject(llvm::Module &llvmModule) {
555555
return SmallVector<char, 0>(bin.begin(), bin.end());
556556
}
557557

558-
// Compile to binary.
558+
// Compile to binary.
559559
#if MLIR_NVPTXCOMPILER_ENABLED == 1
560560
return compileToBinaryNVPTX(*serializedISA);
561561
#else
562562
return compileToBinary(*serializedISA);
563563
#endif // MLIR_NVPTXCOMPILER_ENABLED == 1
564564
}
565-
#endif // MLIR_CUDA_CONVERSIONS_ENABLED == 1
565+
#endif // MLIR_CUDA_CONVERSIONS_ENABLED
566566

567567
std::optional<SmallVector<char, 0>>
568568
NVVMTargetAttrImpl::serializeToObject(Attribute attribute, Operation *module,
@@ -574,15 +574,15 @@ NVVMTargetAttrImpl::serializeToObject(Attribute attribute, Operation *module,
574574
module->emitError("Module must be a GPU module.");
575575
return std::nullopt;
576576
}
577-
#if MLIR_CUDA_CONVERSIONS_ENABLED == 1
577+
#ifdef MLIR_CUDA_CONVERSIONS_ENABLED
578578
NVPTXSerializer serializer(*module, cast<NVVMTargetAttr>(attribute), options);
579579
serializer.init();
580580
return serializer.run();
581581
#else
582582
module->emitError(
583583
"The `NVPTX` target was not built. Please enable it when building LLVM.");
584584
return std::nullopt;
585-
#endif // MLIR_CUDA_CONVERSIONS_ENABLED == 1
585+
#endif // MLIR_CUDA_CONVERSIONS_ENABLED
586586
}
587587

588588
Attribute

mlir/lib/Target/LLVM/ROCDL/Target.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ void SerializeGPUModuleBase::init() {
120120
static llvm::once_flag initializeBackendOnce;
121121
llvm::call_once(initializeBackendOnce, []() {
122122
// If the `AMDGPU` LLVM target was built, initialize it.
123-
#if MLIR_ROCM_CONVERSIONS_ENABLED == 1
123+
#ifdef MLIR_ROCM_CONVERSIONS_ENABLED
124124
LLVMInitializeAMDGPUTarget();
125125
LLVMInitializeAMDGPUTargetInfo();
126126
LLVMInitializeAMDGPUTargetMC();
@@ -318,7 +318,7 @@ SerializeGPUModuleBase::assembleIsa(StringRef isa) {
318318
return result;
319319
}
320320

321-
#if MLIR_ROCM_CONVERSIONS_ENABLED == 1
321+
#ifdef MLIR_ROCM_CONVERSIONS_ENABLED
322322
namespace {
323323
class AMDGPUSerializer : public SerializeGPUModuleBase {
324324
public:
@@ -462,7 +462,7 @@ std::optional<SmallVector<char, 0>> ROCDLTargetAttrImpl::serializeToObject(
462462
module->emitError("Module must be a GPU module.");
463463
return std::nullopt;
464464
}
465-
#if MLIR_ROCM_CONVERSIONS_ENABLED == 1
465+
#ifdef MLIR_ROCM_CONVERSIONS_ENABLED
466466
AMDGPUSerializer serializer(*module, cast<ROCDLTargetAttr>(attribute),
467467
options);
468468
serializer.init();
@@ -471,7 +471,7 @@ std::optional<SmallVector<char, 0>> ROCDLTargetAttrImpl::serializeToObject(
471471
module->emitError("The `AMDGPU` target was not built. Please enable it when "
472472
"building LLVM.");
473473
return std::nullopt;
474-
#endif // MLIR_ROCM_CONVERSIONS_ENABLED == 1
474+
#endif // MLIR_ROCM_CONVERSIONS_ENABLED
475475
}
476476

477477
Attribute

mlir/unittests/Target/LLVM/SerializeNVVMTarget.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
using namespace mlir;
3030

3131
// Skip the test if the NVPTX target was not built.
32-
#if MLIR_CUDA_CONVERSIONS_ENABLED == 0
32+
#ifndef MLIR_CUDA_CONVERSIONS_ENABLED
3333
#define SKIP_WITHOUT_NVPTX(x) DISABLED_##x
3434
#else
3535
#define SKIP_WITHOUT_NVPTX(x) x

mlir/unittests/Target/LLVM/SerializeROCDLTarget.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
using namespace mlir;
3232

3333
// Skip the test if the AMDGPU target was not built.
34-
#if MLIR_ROCM_CONVERSIONS_ENABLED == 0
34+
#ifndef MLIR_ROCM_CONVERSIONS_ENABLED
3535
#define SKIP_WITHOUT_AMDGPU(x) DISABLED_##x
3636
#else
3737
#define SKIP_WITHOUT_AMDGPU(x) x

0 commit comments

Comments
 (0)