[llvm] Fix macro expansion for AMDHSA_BITS_SET (PR #85661)
Janek van Oirschot via llvm-commits
llvm-commits at lists.llvm.org
Mon Mar 18 09:41:01 PDT 2024
https://github.com/JanekvO created https://github.com/llvm/llvm-project/pull/85661
Corrects the `AMDHSA_BITS_SET` macro.
The `VAL` argument for the `AMDHSA_BITS_SET` macro is not encapsulated which may result in unintended expansion. An example of this can be observed in https://github.com/llvm/llvm-project/blob/f8042171552ca16e77a5e9367188e7f218474380/llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.cpp#L1242-L1244 where the macro expands into
```
KD.kernel_code_properties &= ~amdhsa::KERNEL_CODE_PROPERTY_ENABLE_WAVEFRONT_SIZE32;
KD.kernel_code_properties |= ((STI->getFeatureBits().test(FeatureWavefrontSize32) ? 1 : 0 << amdhsa::KERNEL_CODE_PROPERTY_ENABLE_WAVEFRONT_SIZE32_SHIFT) & amdhsa::KERNEL_CODE_PROPERTY_ENABLE_WAVEFRONT_SIZE32)
```
>From ac2b1f352a8876f3d2179811bd3d26a1781e3170 Mon Sep 17 00:00:00 2001
From: Janek van Oirschot <janek.vanoirschot at amd.com>
Date: Mon, 18 Mar 2024 16:23:04 +0000
Subject: [PATCH] Fix macro expansion for AMDHSA_BITS_SET
---
llvm/include/llvm/Support/AMDHSAKernelDescriptor.h | 9 ++++++---
llvm/test/MC/AMDGPU/hsa-gfx12-v4.s | 6 +++---
2 files changed, 9 insertions(+), 6 deletions(-)
diff --git a/llvm/include/llvm/Support/AMDHSAKernelDescriptor.h b/llvm/include/llvm/Support/AMDHSAKernelDescriptor.h
index 84cac3ef700e05..a119b0724d677f 100644
--- a/llvm/include/llvm/Support/AMDHSAKernelDescriptor.h
+++ b/llvm/include/llvm/Support/AMDHSAKernelDescriptor.h
@@ -46,9 +46,12 @@
// Sets bits for specified bit mask in specified destination.
#ifndef AMDHSA_BITS_SET
-#define AMDHSA_BITS_SET(DST, MSK, VAL) \
- DST &= ~MSK; \
- DST |= ((VAL << MSK ## _SHIFT) & MSK)
+#define AMDHSA_BITS_SET(DST, MSK, VAL) \
+ do { \
+ auto local = VAL; \
+ DST &= ~MSK; \
+ DST |= ((local << MSK##_SHIFT) & MSK); \
+ } while (0)
#endif // AMDHSA_BITS_SET
namespace llvm {
diff --git a/llvm/test/MC/AMDGPU/hsa-gfx12-v4.s b/llvm/test/MC/AMDGPU/hsa-gfx12-v4.s
index 8b90e20bb87d1f..7b591904e877fd 100644
--- a/llvm/test/MC/AMDGPU/hsa-gfx12-v4.s
+++ b/llvm/test/MC/AMDGPU/hsa-gfx12-v4.s
@@ -29,7 +29,7 @@
// OBJDUMP-NEXT: 0000 00000000 00000000 00000000 00000000
// OBJDUMP-NEXT: 0010 00000000 00000000 00000000 00000000
// OBJDUMP-NEXT: 0020 00000000 00000000 00000000 00000000
-// OBJDUMP-NEXT: 0030 00000c60 80000000 00000000 00000000
+// OBJDUMP-NEXT: 0030 00000c60 80000000 00040000 00000000
// complete
// OBJDUMP-NEXT: 0040 01000000 01000000 08000000 00000000
// OBJDUMP-NEXT: 0050 00000000 00000000 00000000 00000000
@@ -39,12 +39,12 @@
// OBJDUMP-NEXT: 0080 00000000 00000000 00000000 00000000
// OBJDUMP-NEXT: 0090 00000000 00000000 00000000 00000000
// OBJDUMP-NEXT: 00a0 00000000 00000000 00000000 00000000
-// OBJDUMP-NEXT: 00b0 00000060 80000000 00000000 00000000
+// OBJDUMP-NEXT: 00b0 00000060 80000000 00040000 00000000
// disabled_user_sgpr
// OBJDUMP-NEXT: 00c0 00000000 00000000 00000000 00000000
// OBJDUMP-NEXT: 00d0 00000000 00000000 00000000 00000000
// OBJDUMP-NEXT: 00e0 00000000 00000000 00000000 00000000
-// OBJDUMP-NEXT: 00f0 00000c60 80000000 00000000 00000000
+// OBJDUMP-NEXT: 00f0 00000c60 80000000 00040000 00000000
.text
// ASM: .text
More information about the llvm-commits
mailing list