https://github.com/yxsamliu updated https://github.com/llvm/llvm-project/pull/241042
>From 773911fdb5c1939bb53e145e3fd56825b87b1128 Mon Sep 17 00:00:00 2001 From: Yaxun Liu <[email protected]> Date: Tue, 10 Nov 2026 16:26:07 -0500 Subject: [PATCH 1/6] [AMDGPU] Fix monitor-sleep duration validation on GFX12.5 On GFX12.5, s_monitor_sleep uses only seven bits for the duration. PR #232561 corrected the fallback for unsupported sleep forever to 127, but that path only handles operands with bit 15 set. Ordinary durations such as 128 and 200 remained unchecked. Reject reserved duration bits in builtin calls and assembly, and clamp invalid durations from direct IR to 127. Model the duration restriction separately from sleep-forever support so valid sleep-forever operands remain accepted on targets that support them. --- clang/lib/Basic/Targets/AMDGPU.cpp | 12 +++++ clang/lib/Sema/SemaAMDGPU.cpp | 18 +++++++ .../SemaCUDA/amdgpu-monitor-sleep-range.cu | 32 ++++++++++++ .../builtins-amdgcn-error-gfx1250-param.cl | 10 ++++ .../Target/AMDGPU/AMDGPULowerIntrinsics.cpp | 17 ++++--- .../AMDGPU/AsmParser/AMDGPUAsmParser.cpp | 15 ++++-- llvm/lib/Target/AMDGPU/GCNFeatures.td | 7 +++ .../AMDGPU/llvm.amdgcn.s.monitor.sleep.ll | 50 ++++++++++++++++++- llvm/test/MC/AMDGPU/gfx1250_asm_sopp.s | 4 +- .../MC/AMDGPU/monitor-sleep-duration-err.s | 27 ++++++++++ llvm/test/MC/AMDGPU/monitor-sleep-forever.s | 20 ++++++++ 11 files changed, 197 insertions(+), 15 deletions(-) create mode 100644 clang/test/SemaCUDA/amdgpu-monitor-sleep-range.cu create mode 100644 llvm/test/MC/AMDGPU/monitor-sleep-duration-err.s create mode 100644 llvm/test/MC/AMDGPU/monitor-sleep-forever.s diff --git a/clang/lib/Basic/Targets/AMDGPU.cpp b/clang/lib/Basic/Targets/AMDGPU.cpp index c3e6f8bbcafa8..de077627d337d 100644 --- a/clang/lib/Basic/Targets/AMDGPU.cpp +++ b/clang/lib/Basic/Targets/AMDGPU.cpp @@ -181,6 +181,18 @@ bool AMDGPUTargetInfo::initFeatureMap( return false; } + // The target parser does not expose these backend features to Sema. + if (getTriple().isAMDGCN()) { + llvm::AMDGPU::GPUKind Kind = + CPU.empty() ? getGPUKindFromSubArch(getTriple().getSubArch()) + : parseArchAMDGCN(CPU); + if (Kind == GK_GFX1250 || Kind == GK_GFX1250_STRICT) + Features.insert({"no-sleep-forever", true}); + if (Kind == GK_GFX1250 || Kind == GK_GFX1250_STRICT || Kind == GK_GFX1251 || + Kind == GK_GFX12_5_GENERIC) + Features.insert({"monitor-sleep-7-bit-duration", true}); + } + return true; } diff --git a/clang/lib/Sema/SemaAMDGPU.cpp b/clang/lib/Sema/SemaAMDGPU.cpp index 3742157d2b312..c27a33a3bd38c 100644 --- a/clang/lib/Sema/SemaAMDGPU.cpp +++ b/clang/lib/Sema/SemaAMDGPU.cpp @@ -166,6 +166,24 @@ bool SemaAMDGPU::CheckAMDGCNBuiltinFunctionCall(const TargetInfo &TI, return false; } + case AMDGPU::BI__builtin_amdgcn_s_monitor_sleep: { + bool Has7BitDuration = Builtin::evaluateRequiredTargetFeatures( + "monitor-sleep-7-bit-duration", CallerFeatureMap); + bool NoSleepForever = Builtin::evaluateRequiredTargetFeatures( + "no-sleep-forever", CallerFeatureMap); + if (!Has7BitDuration && !NoSleepForever) + return false; + Expr *ArgExpr = TheCall->getArg(0); + if (ArgExpr->isInstantiationDependent()) + return false; + llvm::APSInt Sleep; + if (SemaRef.BuiltinConstantArg(TheCall, 0, Sleep)) + return true; + // Bit 15 reaches Sema as the sign bit of the builtin's short argument. + int Low = !NoSleepForever && Sleep.isNegative() ? INT16_MIN : 0; + int High = Has7BitDuration ? Low + 127 : INT16_MAX; + return SemaRef.BuiltinConstantArgRange(TheCall, 0, Low, High); + } case AMDGPU::BI__builtin_amdgcn_flat_prefetch: case AMDGPU::BI__builtin_amdgcn_global_prefetch: { // Only th (bits 0-2, not 7), scope (bits 3-4) and nv (bit 5) may be set. diff --git a/clang/test/SemaCUDA/amdgpu-monitor-sleep-range.cu b/clang/test/SemaCUDA/amdgpu-monitor-sleep-range.cu new file mode 100644 index 0000000000000..8517935eeff6a --- /dev/null +++ b/clang/test/SemaCUDA/amdgpu-monitor-sleep-range.cu @@ -0,0 +1,32 @@ +// NOTE: Do not autogenerate. -verify checks target-specific diagnostics. +// REQUIRES: amdgpu-registered-target +// RUN: %clang_cc1 -triple amdgpu12.50-amd-amdhsa -fcuda-is-device -x hip -DNO_SLEEP_FOREVER -fsyntax-only -verify=range,forever %s +// RUN: %clang_cc1 -triple amdgpu12.50s-amd-amdhsa -fcuda-is-device -x hip -DNO_SLEEP_FOREVER -fsyntax-only -verify=range,forever %s +// RUN: %clang_cc1 -triple amdgpu12.51-amd-amdhsa -fcuda-is-device -x hip -fsyntax-only -verify=range,negative %s +// RUN: %clang_cc1 -triple amdgpu12.50-amd-amdhsa -target-feature -no-sleep-forever -fcuda-is-device -x hip -fsyntax-only -verify=range,negative %s +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -aux-triple amdgpu12.50-amd-amdhsa -x hip -DNO_SLEEP_FOREVER -fsyntax-only -verify=host %s +// RUN: %clang_cc1 -triple amdgpu12.5-amd-amdhsa -fcuda-is-device -x hip -fsyntax-only -verify=range,negative %s +// host-no-diagnostics + +#include "Inputs/cuda.h" + +__device__ void monitor_sleep_range() { + __builtin_amdgcn_s_monitor_sleep(0); + __builtin_amdgcn_s_monitor_sleep(127); + __builtin_amdgcn_s_monitor_sleep(128); // range-error {{argument value 128 is outside the valid range [0, 127]}} + __builtin_amdgcn_s_monitor_sleep(200); // range-error {{argument value 200 is outside the valid range [0, 127]}} + __builtin_amdgcn_s_monitor_sleep(32767); // range-error {{argument value 32767 is outside the valid range [0, 127]}} +#ifdef NO_SLEEP_FOREVER + __builtin_amdgcn_s_monitor_sleep(-32768); // forever-error {{argument value -32768 is outside the valid range [0, 127]}} +#else + __builtin_amdgcn_s_monitor_sleep(-32768); + __builtin_amdgcn_s_monitor_sleep(-32641); + __builtin_amdgcn_s_monitor_sleep(-32640); // negative-error {{argument value -32640 is outside the valid range [-32768, -32641]}} + __builtin_amdgcn_s_monitor_sleep(-1); // negative-error {{argument value -1 is outside the valid range [-32768, -32641]}} +#endif +} + +template <short Duration> __device__ void monitor_sleep_template() { + __builtin_amdgcn_s_monitor_sleep(Duration); +} +template __device__ void monitor_sleep_template<127>(); diff --git a/clang/test/SemaOpenCL/builtins-amdgcn-error-gfx1250-param.cl b/clang/test/SemaOpenCL/builtins-amdgcn-error-gfx1250-param.cl index d994d431e1b91..8ba9a57c7cf19 100644 --- a/clang/test/SemaOpenCL/builtins-amdgcn-error-gfx1250-param.cl +++ b/clang/test/SemaOpenCL/builtins-amdgcn-error-gfx1250-param.cl @@ -1,5 +1,7 @@ +// NOTE: Do not autogenerate. -verify checks target-specific diagnostics. // REQUIRES: amdgpu-registered-target // RUN: %clang_cc1 -cl-std=CL2.0 -triple amdgpu12.50-- -verify -S -o - %s +// RUN: %clang_cc1 -cl-std=CL2.0 -triple amdgcn-amd-amdhsa -target-cpu gfx1250-strict -verify -S -o - %s typedef unsigned int uint; typedef unsigned short int ushort; @@ -36,6 +38,14 @@ void test_s_monitor_sleep(short a) { __builtin_amdgcn_s_monitor_sleep(a); // expected-error {{'__builtin_amdgcn_s_monitor_sleep' must be a constant integer}} } +void test_s_monitor_sleep_range() { + __builtin_amdgcn_s_monitor_sleep(0); + __builtin_amdgcn_s_monitor_sleep(127); + __builtin_amdgcn_s_monitor_sleep(128); // expected-error {{argument value 128 is outside the valid range}} + __builtin_amdgcn_s_monitor_sleep(200); // expected-error {{argument value 200 is outside the valid range}} + __builtin_amdgcn_s_monitor_sleep(0x7fff); // expected-error {{argument value 32767 is outside the valid range}} +} + void test_s_wait_asynccnt(short a) { __builtin_amdgcn_s_wait_asynccnt(a); // expected-error {{'__builtin_amdgcn_s_wait_asynccnt' must be a constant integer}} } diff --git a/llvm/lib/Target/AMDGPU/AMDGPULowerIntrinsics.cpp b/llvm/lib/Target/AMDGPU/AMDGPULowerIntrinsics.cpp index 6bd29173ecff3..b67081849ebb6 100644 --- a/llvm/lib/Target/AMDGPU/AMDGPULowerIntrinsics.cpp +++ b/llvm/lib/Target/AMDGPU/AMDGPULowerIntrinsics.cpp @@ -268,17 +268,18 @@ bool AMDGPULowerIntrinsicsImpl::visitMonitorSleep(IntrinsicInst &I) { assert(I.getIntrinsicID() == Intrinsic::amdgcn_s_monitor_sleep); const GCNSubtarget &ST = TM.getSubtarget<GCNSubtarget>(*I.getFunction()); - if (!ST.hasNoSleepForever()) - return false; - - int Sleep = cast<ConstantInt>(I.getArgOperand(0))->getSExtValue(); - if (!(Sleep & 0x8000)) + unsigned Sleep = cast<ConstantInt>(I.getArgOperand(0))->getZExtValue(); + unsigned NewSleep; + if (ST.hasNoSleepForever() && (Sleep & 0x8000)) { + NewSleep = 127; + } else if (ST.hasMonitorSleep7BitDuration() && (Sleep & 0x7f80)) { + NewSleep = (Sleep & 0x8000) | 127; + } else { return false; + } IRBuilder<> B(&I); - Value *NewSleep = B.getInt16(127); // Maximum - I.setArgOperand(0, NewSleep); - + I.setArgOperand(0, B.getInt16(NewSleep)); return true; } diff --git a/llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp b/llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp index c91da6d5200e4..73c82cbf6b1bc 100644 --- a/llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp +++ b/llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp @@ -5559,16 +5559,25 @@ bool AMDGPUAsmParser::validateWMMA(const MCInst &Inst, bool AMDGPUAsmParser::validateMonitorSleep(const MCInst &Inst, const OperandVector &Operands) { unsigned Opc = Inst.getOpcode(); - if (Opc != AMDGPU::S_MONITOR_SLEEP_gfx12 || - !getSTI().hasFeature(AMDGPU::FeatureNoSleepForever)) + if (Opc != AMDGPU::S_MONITOR_SLEEP_gfx12) return true; int ImmIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::simm16); - if (Inst.getOperand(ImmIdx).getImm() & 0x8000) { + int64_t Sleep = Inst.getOperand(ImmIdx).getImm(); + // Accept the sign-extended spelling emitted for negative i16 operands. + if (isUInt<32>(Sleep) && isInt<16>(SignExtend64<32>(Sleep))) + Sleep = SignExtend64<32>(Sleep); + if (getSTI().hasFeature(AMDGPU::FeatureNoSleepForever) && (Sleep & 0x8000)) { Error(getOperandLoc(Operands, ImmIdx), "sleep forever is unsupported on the target"); return false; } + if (getSTI().hasFeature(AMDGPU::FeatureMonitorSleep7BitDuration) && + (Sleep < INT16_MIN || Sleep > UINT16_MAX || (Sleep & 0x7f80))) { + Error(getOperandLoc(Operands, ImmIdx), + "sleep duration must be in range [0, 127] on this target"); + return false; + } return true; } diff --git a/llvm/lib/Target/AMDGPU/GCNFeatures.td b/llvm/lib/Target/AMDGPU/GCNFeatures.td index 2fe5047941c1b..d076a5e7df044 100644 --- a/llvm/lib/Target/AMDGPU/GCNFeatures.td +++ b/llvm/lib/Target/AMDGPU/GCNFeatures.td @@ -1695,6 +1695,12 @@ defm NeedsAligned2addrDS : AMDGPUSubtargetFeature<"needs-aligned-2addr-lds", /*GenPredicate=*/1 >; +defm MonitorSleep7BitDuration : AMDGPUSubtargetFeature< + "monitor-sleep-7-bit-duration", + "s_monitor_sleep uses a 7-bit duration", + /*GenPredicate=*/0 +>; + defm NoSleepForever : AMDGPUSubtargetFeature<"no-sleep-forever", "s_monitor_sleep should not use sleep_forever", /*GenPredicate=*/1 @@ -2503,6 +2509,7 @@ def FeatureISAVersion12 : FeatureSet< def FeatureISAVersion12_50_Common : FeatureSet< [FeatureGFX12, + FeatureMonitorSleep7BitDuration, FeatureGFX1250Insts, FeatureBackOffBarrier, FeatureRequiresAlignedVGPRs, diff --git a/llvm/test/CodeGen/AMDGPU/llvm.amdgcn.s.monitor.sleep.ll b/llvm/test/CodeGen/AMDGPU/llvm.amdgcn.s.monitor.sleep.ll index 9a56b8655962a..67966843c1eda 100644 --- a/llvm/test/CodeGen/AMDGPU/llvm.amdgcn.s.monitor.sleep.ll +++ b/llvm/test/CodeGen/AMDGPU/llvm.amdgcn.s.monitor.sleep.ll @@ -1,7 +1,16 @@ +; NOTE: Do not autogenerate. Full function checks obscure the target-dependent immediate. ; RUN: llc -global-isel=0 -mtriple=amdgpu12.50 < %s | FileCheck --check-prefixes=GCN,GFX1250 %s -; RUN: llc -global-isel=1 -mtriple=amdgpu12.50 < %s | FileCheck --check-prefixes=GCN,GFX1250 %s +; RUN: llc -global-isel=1 -global-isel-abort=1 -mtriple=amdgpu12.50 < %s | FileCheck --check-prefixes=GCN,GFX1250 %s +; RUN: llc -global-isel=0 -mtriple=amdgpu12.50s < %s | FileCheck --check-prefixes=GCN,GFX1250 %s +; RUN: llc -global-isel=1 -global-isel-abort=1 -mtriple=amdgpu12.50s < %s | FileCheck --check-prefixes=GCN,GFX1250 %s ; RUN: llc -global-isel=0 -mtriple=amdgpu12.51 < %s | FileCheck --check-prefixes=GCN,GFX1251 %s -; RUN: llc -global-isel=1 -mtriple=amdgpu12.51 < %s | FileCheck --check-prefixes=GCN,GFX1251 %s +; RUN: llc -global-isel=1 -global-isel-abort=1 -mtriple=amdgpu12.51 < %s | FileCheck --check-prefixes=GCN,GFX1251 %s + +; RUN: llc -global-isel=0 -mtriple=amdgpu12.50 -mattr=-no-sleep-forever < %s | FileCheck --check-prefixes=GCN,GFX1251 %s +; RUN: llc -global-isel=1 -global-isel-abort=1 -mtriple=amdgpu12.50 -mattr=-no-sleep-forever < %s | FileCheck --check-prefixes=GCN,GFX1251 %s + +; RUN: llc -global-isel=0 -mtriple=amdgpu12.5 < %s | FileCheck --check-prefixes=GCN,GFX1251 %s +; RUN: llc -global-isel=1 -global-isel-abort=1 -mtriple=amdgpu12.5 < %s | FileCheck --check-prefixes=GCN,GFX1251 %s declare void @llvm.amdgcn.s.monitor.sleep(i16) @@ -12,6 +21,27 @@ define amdgpu_ps void @test_monitor_sleep_1() { ret void } +; GCN-LABEL: {{^}}test_monitor_sleep_127: +; GCN: s_monitor_sleep 0x7f +define amdgpu_ps void @test_monitor_sleep_127() { + call void @llvm.amdgcn.s.monitor.sleep(i16 127) + ret void +} + +; GCN-LABEL: {{^}}test_monitor_sleep_128: +; GCN: s_monitor_sleep 0x7f +define amdgpu_ps void @test_monitor_sleep_128() { + call void @llvm.amdgcn.s.monitor.sleep(i16 128) + ret void +} + +; GCN-LABEL: {{^}}test_monitor_sleep_200: +; GCN: s_monitor_sleep 0x7f +define amdgpu_ps void @test_monitor_sleep_200() { + call void @llvm.amdgcn.s.monitor.sleep(i16 200) + ret void +} + ; FIXME: 0x8000 would look better ; GCN-LABEL: {{^}}test_monitor_sleep_forever: @@ -21,3 +51,19 @@ define amdgpu_ps void @test_monitor_sleep_forever() { call void @llvm.amdgcn.s.monitor.sleep(i16 32768) ret void } + +; GCN-LABEL: {{^}}test_monitor_sleep_forever_max_duration: +; GFX1250: s_monitor_sleep 0x7f +; GFX1251: s_monitor_sleep 0xffff807f +define amdgpu_ps void @test_monitor_sleep_forever_max_duration() { + call void @llvm.amdgcn.s.monitor.sleep(i16 32895) + ret void +} + +; GCN-LABEL: {{^}}test_monitor_sleep_forever_reserved: +; GFX1250: s_monitor_sleep 0x7f +; GFX1251: s_monitor_sleep 0xffff807f +define amdgpu_ps void @test_monitor_sleep_forever_reserved() { + call void @llvm.amdgcn.s.monitor.sleep(i16 -1) + ret void +} diff --git a/llvm/test/MC/AMDGPU/gfx1250_asm_sopp.s b/llvm/test/MC/AMDGPU/gfx1250_asm_sopp.s index 4a94d9063b5c0..6e72cd629e1a2 100644 --- a/llvm/test/MC/AMDGPU/gfx1250_asm_sopp.s +++ b/llvm/test/MC/AMDGPU/gfx1250_asm_sopp.s @@ -54,9 +54,9 @@ s_monitor_sleep 1 // GFX12-ERR: :[[@LINE-1]]:1: error: instruction not supported on this GPU (gfx1200): s_monitor_sleep // GFX1250: s_monitor_sleep 1 ; encoding: [0x01,0x00,0x84,0xbf] -s_monitor_sleep 0x2000 +s_monitor_sleep 0x7f // GFX12-ERR: :[[@LINE-1]]:1: error: instruction not supported on this GPU (gfx1200): s_monitor_sleep -// GFX1250: s_monitor_sleep 0x2000 ; encoding: [0x00,0x20,0x84,0xbf] +// GFX1250: s_monitor_sleep 0x7f ; encoding: [0x7f,0x00,0x84,0xbf] s_monitor_sleep 0 // GFX12-ERR: :[[@LINE-1]]:1: error: instruction not supported on this GPU (gfx1200): s_monitor_sleep diff --git a/llvm/test/MC/AMDGPU/monitor-sleep-duration-err.s b/llvm/test/MC/AMDGPU/monitor-sleep-duration-err.s new file mode 100644 index 0000000000000..5aa4d39343c31 --- /dev/null +++ b/llvm/test/MC/AMDGPU/monitor-sleep-duration-err.s @@ -0,0 +1,27 @@ +// NOTE: Assertions have been autogenerated by utils/update_mc_test_checks.py UTC_ARGS: --unique --sort --version 6 +// RUN: not llvm-mc -triple=amdgpu12.5 -filetype=null %s 2>&1 | FileCheck --check-prefixes=ERR,FOREVER --implicit-check-not=error: -strict-whitespace %s + +// RUN: not llvm-mc -triple=amdgpu12.50 -filetype=null %s 2>&1 | FileCheck --check-prefixes=ERR,NO-FOREVER --implicit-check-not=error: -strict-whitespace %s +// RUN: not llvm-mc -triple=amdgpu12.50s -filetype=null %s 2>&1 | FileCheck --check-prefixes=ERR,NO-FOREVER --implicit-check-not=error: -strict-whitespace %s +// RUN: not llvm-mc -triple=amdgpu12.51 -filetype=null %s 2>&1 | FileCheck --check-prefixes=ERR,FOREVER --implicit-check-not=error: -strict-whitespace %s +// RUN: not llvm-mc -triple=amdgpu12.50 -mattr=-no-sleep-forever -filetype=null %s 2>&1 | FileCheck --check-prefixes=ERR,FOREVER --implicit-check-not=error: -strict-whitespace %s + +s_monitor_sleep -1 +// FOREVER: :[[@LINE-1]]:17: error: sleep duration must be in range [0, 127] on this target +// NO-FOREVER: :[[@LINE-2]]:17: error: sleep forever is unsuported on the target + +s_monitor_sleep 0x10000 +// ERR: :[[@LINE-1]]:17: error: sleep duration must be in range [0, 127] on this target + +s_monitor_sleep 0x7fff +// ERR: :[[@LINE-1]]:17: error: sleep duration must be in range [0, 127] on this target + +s_monitor_sleep 0x80 +// ERR: :[[@LINE-1]]:17: error: sleep duration must be in range [0, 127] on this target + +s_monitor_sleep 0x8080 +// FOREVER: :[[@LINE-1]]:17: error: sleep duration must be in range [0, 127] on this target +// NO-FOREVER: :[[@LINE-2]]:17: error: sleep forever is unsuported on the target + +s_monitor_sleep 0xc8 +// ERR: :[[@LINE-1]]:17: error: sleep duration must be in range [0, 127] on this target diff --git a/llvm/test/MC/AMDGPU/monitor-sleep-forever.s b/llvm/test/MC/AMDGPU/monitor-sleep-forever.s new file mode 100644 index 0000000000000..c2f52a8af7b10 --- /dev/null +++ b/llvm/test/MC/AMDGPU/monitor-sleep-forever.s @@ -0,0 +1,20 @@ +// NOTE: Assertions have been autogenerated by utils/update_mc_test_checks.py UTC_ARGS: --version 6 +// RUN: llvm-mc -triple=amdgpu12.51 -show-encoding %s | FileCheck %s +// RUN: llvm-mc -triple=amdgpu12.50 -mattr=-no-sleep-forever -show-encoding %s | FileCheck %s +// RUN: llvm-mc -triple=amdgpu12.50s -mattr=-no-sleep-forever -show-encoding %s | FileCheck %s + +// RUN: llvm-mc -triple=amdgpu12.5 -show-encoding %s | FileCheck %s + +s_monitor_sleep 0x8000 +// CHECK: s_monitor_sleep 0x8000 ; encoding: [0x00,0x80,0x84,0xbf] +s_monitor_sleep 0x807f +// CHECK: s_monitor_sleep 0x807f ; encoding: [0x7f,0x80,0x84,0xbf] +s_monitor_sleep -32768 +// CHECK: s_monitor_sleep 0xffff8000 ; encoding: [0x00,0x80,0x84,0xbf] +s_monitor_sleep -32641 +// CHECK: s_monitor_sleep 0xffff807f ; encoding: [0x7f,0x80,0x84,0xbf] + +s_monitor_sleep 0xffff8000 +// CHECK: s_monitor_sleep 0xffff8000 ; encoding: [0x00,0x80,0x84,0xbf] +s_monitor_sleep 0xffff807f +// CHECK: s_monitor_sleep 0xffff807f ; encoding: [0x7f,0x80,0x84,0xbf] >From a556676433a902e93eec292e071711f76ec5ea85 Mon Sep 17 00:00:00 2001 From: Yaxun Liu <[email protected]> Date: Wed, 11 Nov 2026 12:43:39 +0000 Subject: [PATCH 2/6] [AMDGPU] Address monitor-sleep review feedback Expose the monitor-sleep features through the shared frontend feature table and remove manual architecture parsing. Check the extracted duration against its target range, with the sleep-forever bit handled separately. Reject invalid durations in target instruction verification for both pseudo and encoded monitor-sleep instructions. Preserve the existing sleep-forever fallback and the wider durations supported by GFX13. Add direct-IR and MIR coverage for rejection and valid boundaries. --- clang/lib/Basic/Targets/AMDGPU.cpp | 12 --- .../builtins-amdgcn-error-gfx1250-param.cl | 2 +- .../Target/AMDGPU/AMDGPULowerIntrinsics.cpp | 17 ++-- .../AMDGPU/AsmParser/AMDGPUAsmParser.cpp | 2 +- llvm/lib/Target/AMDGPU/GCNFeatures.td | 3 +- llvm/lib/Target/AMDGPU/SIInstrInfo.cpp | 20 +++++ .../AMDGPU/llvm.amdgcn.s.monitor.sleep.ll | 25 +----- .../CodeGen/AMDGPU/monitor-sleep-gfx13.ll | 14 ++++ .../AMDGPU/monitor-sleep-invalid-forever.ll | 18 +++++ .../CodeGen/AMDGPU/monitor-sleep-invalid.ll | 25 ++++++ .../CodeGen/AMDGPU/monitor-sleep-invalid.mir | 77 +++++++++++++++++++ 11 files changed, 169 insertions(+), 46 deletions(-) create mode 100644 llvm/test/CodeGen/AMDGPU/monitor-sleep-gfx13.ll create mode 100644 llvm/test/CodeGen/AMDGPU/monitor-sleep-invalid-forever.ll create mode 100644 llvm/test/CodeGen/AMDGPU/monitor-sleep-invalid.ll create mode 100644 llvm/test/CodeGen/AMDGPU/monitor-sleep-invalid.mir diff --git a/clang/lib/Basic/Targets/AMDGPU.cpp b/clang/lib/Basic/Targets/AMDGPU.cpp index de077627d337d..c3e6f8bbcafa8 100644 --- a/clang/lib/Basic/Targets/AMDGPU.cpp +++ b/clang/lib/Basic/Targets/AMDGPU.cpp @@ -181,18 +181,6 @@ bool AMDGPUTargetInfo::initFeatureMap( return false; } - // The target parser does not expose these backend features to Sema. - if (getTriple().isAMDGCN()) { - llvm::AMDGPU::GPUKind Kind = - CPU.empty() ? getGPUKindFromSubArch(getTriple().getSubArch()) - : parseArchAMDGCN(CPU); - if (Kind == GK_GFX1250 || Kind == GK_GFX1250_STRICT) - Features.insert({"no-sleep-forever", true}); - if (Kind == GK_GFX1250 || Kind == GK_GFX1250_STRICT || Kind == GK_GFX1251 || - Kind == GK_GFX12_5_GENERIC) - Features.insert({"monitor-sleep-7-bit-duration", true}); - } - return true; } diff --git a/clang/test/SemaOpenCL/builtins-amdgcn-error-gfx1250-param.cl b/clang/test/SemaOpenCL/builtins-amdgcn-error-gfx1250-param.cl index 8ba9a57c7cf19..b8bba4e20df76 100644 --- a/clang/test/SemaOpenCL/builtins-amdgcn-error-gfx1250-param.cl +++ b/clang/test/SemaOpenCL/builtins-amdgcn-error-gfx1250-param.cl @@ -1,7 +1,7 @@ // NOTE: Do not autogenerate. -verify checks target-specific diagnostics. // REQUIRES: amdgpu-registered-target // RUN: %clang_cc1 -cl-std=CL2.0 -triple amdgpu12.50-- -verify -S -o - %s -// RUN: %clang_cc1 -cl-std=CL2.0 -triple amdgcn-amd-amdhsa -target-cpu gfx1250-strict -verify -S -o - %s +// RUN: %clang_cc1 -cl-std=CL2.0 -triple amdgpu12.50s-amd-amdhsa -verify -S -o - %s typedef unsigned int uint; typedef unsigned short int ushort; diff --git a/llvm/lib/Target/AMDGPU/AMDGPULowerIntrinsics.cpp b/llvm/lib/Target/AMDGPU/AMDGPULowerIntrinsics.cpp index b67081849ebb6..6bd29173ecff3 100644 --- a/llvm/lib/Target/AMDGPU/AMDGPULowerIntrinsics.cpp +++ b/llvm/lib/Target/AMDGPU/AMDGPULowerIntrinsics.cpp @@ -268,18 +268,17 @@ bool AMDGPULowerIntrinsicsImpl::visitMonitorSleep(IntrinsicInst &I) { assert(I.getIntrinsicID() == Intrinsic::amdgcn_s_monitor_sleep); const GCNSubtarget &ST = TM.getSubtarget<GCNSubtarget>(*I.getFunction()); - unsigned Sleep = cast<ConstantInt>(I.getArgOperand(0))->getZExtValue(); - unsigned NewSleep; - if (ST.hasNoSleepForever() && (Sleep & 0x8000)) { - NewSleep = 127; - } else if (ST.hasMonitorSleep7BitDuration() && (Sleep & 0x7f80)) { - NewSleep = (Sleep & 0x8000) | 127; - } else { + if (!ST.hasNoSleepForever()) + return false; + + int Sleep = cast<ConstantInt>(I.getArgOperand(0))->getSExtValue(); + if (!(Sleep & 0x8000)) return false; - } IRBuilder<> B(&I); - I.setArgOperand(0, B.getInt16(NewSleep)); + Value *NewSleep = B.getInt16(127); // Maximum + I.setArgOperand(0, NewSleep); + return true; } diff --git a/llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp b/llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp index 73c82cbf6b1bc..5b58a3261774d 100644 --- a/llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp +++ b/llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp @@ -5573,7 +5573,7 @@ bool AMDGPUAsmParser::validateMonitorSleep(const MCInst &Inst, return false; } if (getSTI().hasFeature(AMDGPU::FeatureMonitorSleep7BitDuration) && - (Sleep < INT16_MIN || Sleep > UINT16_MAX || (Sleep & 0x7f80))) { + (Sleep < INT16_MIN || Sleep > UINT16_MAX || (Sleep & 0x7fff) > 127)) { Error(getOperandLoc(Operands, ImmIdx), "sleep duration must be in range [0, 127] on this target"); return false; diff --git a/llvm/lib/Target/AMDGPU/GCNFeatures.td b/llvm/lib/Target/AMDGPU/GCNFeatures.td index d076a5e7df044..9e38e4ae516ba 100644 --- a/llvm/lib/Target/AMDGPU/GCNFeatures.td +++ b/llvm/lib/Target/AMDGPU/GCNFeatures.td @@ -2862,7 +2862,8 @@ def AMDGPUFrontendVisibleFeatures { FeatureGFX940Insts, FeatureGFX950Insts, FeatureGFX9Insts, FeatureGWS, FeatureImageInsts, FeatureLdsBarrierArriveAtomic, FeatureLerpInst, FeatureLoadMonitorInsts, FeatureMAIInsts, FeatureMcastLoadInsts, - FeatureMqsadInsts, FeatureMqsadPkInsts, FeatureMsadInsts, + FeatureMonitorSleep7BitDuration, FeatureMqsadInsts, FeatureMqsadPkInsts, + FeatureMsadInsts, FeatureNoSleepForever, FeatureOCPFP8ConversionInsts, FeaturePermlane16Insts, FeaturePermlane16Swap, FeaturePermlane32Swap, FeaturePkAddMinMaxInsts, FeaturePrngInst, FeatureQsadInsts, FeatureSMemRealTime, FeatureSMemTimeInst, diff --git a/llvm/lib/Target/AMDGPU/SIInstrInfo.cpp b/llvm/lib/Target/AMDGPU/SIInstrInfo.cpp index 63ae0f06f4c9b..a93372c471410 100644 --- a/llvm/lib/Target/AMDGPU/SIInstrInfo.cpp +++ b/llvm/lib/Target/AMDGPU/SIInstrInfo.cpp @@ -5605,6 +5605,26 @@ bool SIInstrInfo::verifyInstruction(const MachineInstr &MI, return false; } + if (Opcode == AMDGPU::S_MONITOR_SLEEP || + Opcode == AMDGPU::S_MONITOR_SLEEP_gfx12 || + Opcode == AMDGPU::S_MONITOR_SLEEP_gfx13) { + const MachineOperand &Sleep = MI.getOperand(0); + if (!Sleep.isImm()) { + ErrInfo = "sleep duration must be an immediate"; + return false; + } + int64_t Value = Sleep.getImm(); + if (ST.hasNoSleepForever() && (Value & 0x8000)) { + ErrInfo = "sleep forever is unsupported on this target"; + return false; + } + if (ST.hasMonitorSleep7BitDuration() && + ((!isInt<16>(Value) && !isUInt<16>(Value)) || (Value & 0x7fff) > 127)) { + ErrInfo = "sleep duration must be in range [0, 127] on this target"; + return false; + } + } + if (MI.isInlineAsm()) { // Verify register classes for inlineasm constraints. for (unsigned I = InlineAsm::MIOp_FirstOperand, E = MI.getNumOperands(); diff --git a/llvm/test/CodeGen/AMDGPU/llvm.amdgcn.s.monitor.sleep.ll b/llvm/test/CodeGen/AMDGPU/llvm.amdgcn.s.monitor.sleep.ll index 67966843c1eda..c9119129b9ff0 100644 --- a/llvm/test/CodeGen/AMDGPU/llvm.amdgcn.s.monitor.sleep.ll +++ b/llvm/test/CodeGen/AMDGPU/llvm.amdgcn.s.monitor.sleep.ll @@ -9,6 +9,9 @@ ; RUN: llc -global-isel=0 -mtriple=amdgpu12.50 -mattr=-no-sleep-forever < %s | FileCheck --check-prefixes=GCN,GFX1251 %s ; RUN: llc -global-isel=1 -global-isel-abort=1 -mtriple=amdgpu12.50 -mattr=-no-sleep-forever < %s | FileCheck --check-prefixes=GCN,GFX1251 %s +; RUN: llc -global-isel=0 -mtriple=amdgpu13 < %s | FileCheck --check-prefixes=GCN,GFX1251 %s +; RUN: llc -global-isel=1 -global-isel-abort=1 -mtriple=amdgpu13 < %s | FileCheck --check-prefixes=GCN,GFX1251 %s + ; RUN: llc -global-isel=0 -mtriple=amdgpu12.5 < %s | FileCheck --check-prefixes=GCN,GFX1251 %s ; RUN: llc -global-isel=1 -global-isel-abort=1 -mtriple=amdgpu12.5 < %s | FileCheck --check-prefixes=GCN,GFX1251 %s @@ -28,20 +31,6 @@ define amdgpu_ps void @test_monitor_sleep_127() { ret void } -; GCN-LABEL: {{^}}test_monitor_sleep_128: -; GCN: s_monitor_sleep 0x7f -define amdgpu_ps void @test_monitor_sleep_128() { - call void @llvm.amdgcn.s.monitor.sleep(i16 128) - ret void -} - -; GCN-LABEL: {{^}}test_monitor_sleep_200: -; GCN: s_monitor_sleep 0x7f -define amdgpu_ps void @test_monitor_sleep_200() { - call void @llvm.amdgcn.s.monitor.sleep(i16 200) - ret void -} - ; FIXME: 0x8000 would look better ; GCN-LABEL: {{^}}test_monitor_sleep_forever: @@ -59,11 +48,3 @@ define amdgpu_ps void @test_monitor_sleep_forever_max_duration() { call void @llvm.amdgcn.s.monitor.sleep(i16 32895) ret void } - -; GCN-LABEL: {{^}}test_monitor_sleep_forever_reserved: -; GFX1250: s_monitor_sleep 0x7f -; GFX1251: s_monitor_sleep 0xffff807f -define amdgpu_ps void @test_monitor_sleep_forever_reserved() { - call void @llvm.amdgcn.s.monitor.sleep(i16 -1) - ret void -} diff --git a/llvm/test/CodeGen/AMDGPU/monitor-sleep-gfx13.ll b/llvm/test/CodeGen/AMDGPU/monitor-sleep-gfx13.ll new file mode 100644 index 0000000000000..92de1c9582fbf --- /dev/null +++ b/llvm/test/CodeGen/AMDGPU/monitor-sleep-gfx13.ll @@ -0,0 +1,14 @@ +; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6 +; RUN: llc -global-isel=0 -mtriple=amdgpu13 < %s | FileCheck %s +; RUN: llc -global-isel=1 -global-isel-abort=1 -mtriple=amdgpu13 < %s | FileCheck %s + +declare void @llvm.amdgcn.s.monitor.sleep(i16) + +define amdgpu_ps void @sleep_duration() { +; CHECK-LABEL: sleep_duration: +; CHECK: ; %bb.0: +; CHECK-NEXT: s_monitor_sleep 0x1234 +; CHECK-NEXT: s_endpgm + call void @llvm.amdgcn.s.monitor.sleep(i16 4660) + ret void +} diff --git a/llvm/test/CodeGen/AMDGPU/monitor-sleep-invalid-forever.ll b/llvm/test/CodeGen/AMDGPU/monitor-sleep-invalid-forever.ll new file mode 100644 index 0000000000000..1cc9feb06ba45 --- /dev/null +++ b/llvm/test/CodeGen/AMDGPU/monitor-sleep-invalid-forever.ll @@ -0,0 +1,18 @@ +; NOTE: Do not autogenerate. Check diagnostics for reserved duration bits with sleep forever enabled. +; RUN: not llc -global-isel=0 -verify-machineinstrs=0 -mtriple=amdgpu12.51 < %s 2>&1 | FileCheck %s +; RUN: not llc -global-isel=1 -global-isel-abort=1 -verify-machineinstrs=0 -mtriple=amdgpu12.51 < %s 2>&1 | FileCheck %s + +; CHECK: error: Illegal instruction detected: sleep duration must be in range [0, 127] on this target +; CHECK: error: Illegal instruction detected: sleep duration must be in range [0, 127] on this target + +declare void @llvm.amdgcn.s.monitor.sleep(i16) + +define amdgpu_ps void @sleep_forever_128() { + call void @llvm.amdgcn.s.monitor.sleep(i16 -32640) + ret void +} + +define amdgpu_ps void @sleep_forever_reserved() { + call void @llvm.amdgcn.s.monitor.sleep(i16 -1) + ret void +} diff --git a/llvm/test/CodeGen/AMDGPU/monitor-sleep-invalid.ll b/llvm/test/CodeGen/AMDGPU/monitor-sleep-invalid.ll new file mode 100644 index 0000000000000..18314d88a9bdb --- /dev/null +++ b/llvm/test/CodeGen/AMDGPU/monitor-sleep-invalid.ll @@ -0,0 +1,25 @@ +; NOTE: Do not autogenerate. Check the diagnostic from normal instruction emission. +; The target verifier must reject direct IR even when MachineVerifier is disabled. +; RUN: not llc -global-isel=0 -verify-machineinstrs=0 -mtriple=amdgpu12.50 < %s 2>&1 | FileCheck %s +; RUN: not llc -global-isel=1 -global-isel-abort=1 -verify-machineinstrs=0 -mtriple=amdgpu12.50 < %s 2>&1 | FileCheck %s +; RUN: not llc -global-isel=0 -verify-machineinstrs=0 -mtriple=amdgpu12.50s < %s 2>&1 | FileCheck %s +; RUN: not llc -global-isel=1 -global-isel-abort=1 -verify-machineinstrs=0 -mtriple=amdgpu12.50s < %s 2>&1 | FileCheck %s +; RUN: not llc -global-isel=0 -verify-machineinstrs=0 -mtriple=amdgpu12.51 < %s 2>&1 | FileCheck %s +; RUN: not llc -global-isel=1 -global-isel-abort=1 -verify-machineinstrs=0 -mtriple=amdgpu12.51 < %s 2>&1 | FileCheck %s +; RUN: not llc -global-isel=0 -verify-machineinstrs=0 -mtriple=amdgpu12.5 < %s 2>&1 | FileCheck %s +; RUN: not llc -global-isel=1 -global-isel-abort=1 -verify-machineinstrs=0 -mtriple=amdgpu12.5 < %s 2>&1 | FileCheck %s + +; CHECK: error: Illegal instruction detected: sleep duration must be in range [0, 127] on this target +; CHECK: error: Illegal instruction detected: sleep duration must be in range [0, 127] on this target + +declare void @llvm.amdgcn.s.monitor.sleep(i16) + +define amdgpu_ps void @sleep_128() { + call void @llvm.amdgcn.s.monitor.sleep(i16 128) + ret void +} + +define amdgpu_ps void @sleep_200() { + call void @llvm.amdgcn.s.monitor.sleep(i16 200) + ret void +} diff --git a/llvm/test/CodeGen/AMDGPU/monitor-sleep-invalid.mir b/llvm/test/CodeGen/AMDGPU/monitor-sleep-invalid.mir new file mode 100644 index 0000000000000..8f2acb416713c --- /dev/null +++ b/llvm/test/CodeGen/AMDGPU/monitor-sleep-invalid.mir @@ -0,0 +1,77 @@ +# NOTE: Do not autogenerate. Check the target's MachineVerifier diagnostics. +# RUN: split-file %s %t +# RUN: llc -mtriple=amdgpu12.50 -run-pass=none -verify-machineinstrs -filetype=null %t/valid-duration.mir +# RUN: llc -mtriple=amdgpu12.51 -run-pass=none -verify-machineinstrs -filetype=null %t/valid-duration.mir +# RUN: llc -mtriple=amdgpu12.51 -run-pass=none -verify-machineinstrs -filetype=null %t/valid-forever.mir +# RUN: llc -mtriple=amdgpu13 -run-pass=none -verify-machineinstrs -filetype=null %t/gfx13.mir +# RUN: not --crash llc -mtriple=amdgpu12.51 -run-pass=none -verify-machineinstrs -filetype=null %t/duration.mir 2>&1 | FileCheck %s --check-prefix=DURATION +# RUN: not --crash llc -mtriple=amdgpu12.50 -run-pass=none -verify-machineinstrs -filetype=null %t/forever.mir 2>&1 | FileCheck %s --check-prefix=FOREVER + +# DURATION: Bad machine code: sleep duration must be in range [0, 127] on this target +# DURATION: S_MONITOR_SLEEP 200 +# DURATION: Bad machine code: sleep duration must be in range [0, 127] on this target +# DURATION: S_MONITOR_SLEEP 65536 +# DURATION: Bad machine code: sleep duration must be in range [0, 127] on this target +# DURATION: S_MONITOR_SLEEP_gfx12 200 +# DURATION: Bad machine code: sleep duration must be in range [0, 127] on this target +# DURATION: S_MONITOR_SLEEP_gfx12 65536 +# FOREVER: Bad machine code: sleep forever is unsupported on this target +# FOREVER: S_MONITOR_SLEEP 32768 +# FOREVER: Bad machine code: sleep forever is unsupported on this target +# FOREVER: S_MONITOR_SLEEP_gfx12 32768 + +#--- duration.mir +--- +name: sleep_200 +body: | + bb.0: + S_MONITOR_SLEEP 200 + S_MONITOR_SLEEP 65536 + S_MONITOR_SLEEP_gfx12 200 + S_MONITOR_SLEEP_gfx12 65536 + S_ENDPGM 0 +... + +#--- forever.mir +--- +name: sleep_forever +body: | + bb.0: + S_MONITOR_SLEEP 32768 + S_MONITOR_SLEEP_gfx12 32768 + S_ENDPGM 0 +... + +#--- valid-duration.mir +--- +name: valid_duration +body: | + bb.0: + S_MONITOR_SLEEP 0 + S_MONITOR_SLEEP 127 + S_MONITOR_SLEEP_gfx12 0 + S_MONITOR_SLEEP_gfx12 127 + S_ENDPGM 0 +... + +#--- valid-forever.mir +--- +name: valid_forever +body: | + bb.0: + S_MONITOR_SLEEP_gfx12 32768 + S_MONITOR_SLEEP_gfx12 32895 + S_MONITOR_SLEEP_gfx12 -32768 + S_MONITOR_SLEEP_gfx12 -32641 + S_ENDPGM 0 +... + +#--- gfx13.mir +--- +name: wider_duration +body: | + bb.0: + S_MONITOR_SLEEP 4660 + S_MONITOR_SLEEP_gfx13 4660 + S_ENDPGM 0 +... >From c757b017dd059ed706f120e21c38d50cc41f8255 Mon Sep 17 00:00:00 2001 From: Yaxun Liu <[email protected]> Date: Wed, 11 Nov 2026 21:02:09 +0000 Subject: [PATCH 3/6] [AMDGPU] Simplify monitor-sleep tests Remove redundant GlobalISel abort options and intrinsic declarations. Clarify that disabling MachineVerifier tests duration checks during code emission. Preserve the existing inputs and assertions. --- llvm/test/CodeGen/AMDGPU/monitor-sleep-gfx13.ll | 4 +--- .../AMDGPU/monitor-sleep-invalid-forever.ll | 4 +--- llvm/test/CodeGen/AMDGPU/monitor-sleep-invalid.ll | 14 ++++++-------- 3 files changed, 8 insertions(+), 14 deletions(-) diff --git a/llvm/test/CodeGen/AMDGPU/monitor-sleep-gfx13.ll b/llvm/test/CodeGen/AMDGPU/monitor-sleep-gfx13.ll index 92de1c9582fbf..413aee985c302 100644 --- a/llvm/test/CodeGen/AMDGPU/monitor-sleep-gfx13.ll +++ b/llvm/test/CodeGen/AMDGPU/monitor-sleep-gfx13.ll @@ -1,8 +1,6 @@ ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6 ; RUN: llc -global-isel=0 -mtriple=amdgpu13 < %s | FileCheck %s -; RUN: llc -global-isel=1 -global-isel-abort=1 -mtriple=amdgpu13 < %s | FileCheck %s - -declare void @llvm.amdgcn.s.monitor.sleep(i16) +; RUN: llc -global-isel=1 -mtriple=amdgpu13 < %s | FileCheck %s define amdgpu_ps void @sleep_duration() { ; CHECK-LABEL: sleep_duration: diff --git a/llvm/test/CodeGen/AMDGPU/monitor-sleep-invalid-forever.ll b/llvm/test/CodeGen/AMDGPU/monitor-sleep-invalid-forever.ll index 1cc9feb06ba45..9d583e08953ea 100644 --- a/llvm/test/CodeGen/AMDGPU/monitor-sleep-invalid-forever.ll +++ b/llvm/test/CodeGen/AMDGPU/monitor-sleep-invalid-forever.ll @@ -1,12 +1,10 @@ ; NOTE: Do not autogenerate. Check diagnostics for reserved duration bits with sleep forever enabled. ; RUN: not llc -global-isel=0 -verify-machineinstrs=0 -mtriple=amdgpu12.51 < %s 2>&1 | FileCheck %s -; RUN: not llc -global-isel=1 -global-isel-abort=1 -verify-machineinstrs=0 -mtriple=amdgpu12.51 < %s 2>&1 | FileCheck %s +; RUN: not llc -global-isel=1 -verify-machineinstrs=0 -mtriple=amdgpu12.51 < %s 2>&1 | FileCheck %s ; CHECK: error: Illegal instruction detected: sleep duration must be in range [0, 127] on this target ; CHECK: error: Illegal instruction detected: sleep duration must be in range [0, 127] on this target -declare void @llvm.amdgcn.s.monitor.sleep(i16) - define amdgpu_ps void @sleep_forever_128() { call void @llvm.amdgcn.s.monitor.sleep(i16 -32640) ret void diff --git a/llvm/test/CodeGen/AMDGPU/monitor-sleep-invalid.ll b/llvm/test/CodeGen/AMDGPU/monitor-sleep-invalid.ll index 18314d88a9bdb..34cd15a760115 100644 --- a/llvm/test/CodeGen/AMDGPU/monitor-sleep-invalid.ll +++ b/llvm/test/CodeGen/AMDGPU/monitor-sleep-invalid.ll @@ -1,19 +1,17 @@ -; NOTE: Do not autogenerate. Check the diagnostic from normal instruction emission. -; The target verifier must reject direct IR even when MachineVerifier is disabled. +; NOTE: Do not autogenerate. Check diagnostics rather than assembly. +; Disable MachineVerifier to exercise the check during code emission. ; RUN: not llc -global-isel=0 -verify-machineinstrs=0 -mtriple=amdgpu12.50 < %s 2>&1 | FileCheck %s -; RUN: not llc -global-isel=1 -global-isel-abort=1 -verify-machineinstrs=0 -mtriple=amdgpu12.50 < %s 2>&1 | FileCheck %s +; RUN: not llc -global-isel=1 -verify-machineinstrs=0 -mtriple=amdgpu12.50 < %s 2>&1 | FileCheck %s ; RUN: not llc -global-isel=0 -verify-machineinstrs=0 -mtriple=amdgpu12.50s < %s 2>&1 | FileCheck %s -; RUN: not llc -global-isel=1 -global-isel-abort=1 -verify-machineinstrs=0 -mtriple=amdgpu12.50s < %s 2>&1 | FileCheck %s +; RUN: not llc -global-isel=1 -verify-machineinstrs=0 -mtriple=amdgpu12.50s < %s 2>&1 | FileCheck %s ; RUN: not llc -global-isel=0 -verify-machineinstrs=0 -mtriple=amdgpu12.51 < %s 2>&1 | FileCheck %s -; RUN: not llc -global-isel=1 -global-isel-abort=1 -verify-machineinstrs=0 -mtriple=amdgpu12.51 < %s 2>&1 | FileCheck %s +; RUN: not llc -global-isel=1 -verify-machineinstrs=0 -mtriple=amdgpu12.51 < %s 2>&1 | FileCheck %s ; RUN: not llc -global-isel=0 -verify-machineinstrs=0 -mtriple=amdgpu12.5 < %s 2>&1 | FileCheck %s -; RUN: not llc -global-isel=1 -global-isel-abort=1 -verify-machineinstrs=0 -mtriple=amdgpu12.5 < %s 2>&1 | FileCheck %s +; RUN: not llc -global-isel=1 -verify-machineinstrs=0 -mtriple=amdgpu12.5 < %s 2>&1 | FileCheck %s ; CHECK: error: Illegal instruction detected: sleep duration must be in range [0, 127] on this target ; CHECK: error: Illegal instruction detected: sleep duration must be in range [0, 127] on this target -declare void @llvm.amdgcn.s.monitor.sleep(i16) - define amdgpu_ps void @sleep_128() { call void @llvm.amdgcn.s.monitor.sleep(i16 128) ret void >From 147bf554599c06002860ec00174491e71b14ebd7 Mon Sep 17 00:00:00 2001 From: Yaxun Liu <[email protected]> Date: Fri, 13 Nov 2026 01:12:54 +0000 Subject: [PATCH 4/6] [AMDGPU] Apply monitor-sleep duration checks to GFX13 Remove the always-true duration feature and validate the seven-bit duration for all s_monitor_sleep opcode forms. Preserve the target-dependent sleep-forever restriction and HIP host parsing. Cover generic and concrete GFX13 targets in Sema, MC, IR and MIR tests, and replace the old wide-duration acceptance checks with legal boundaries. Assisted-by: OpenAI Codex --- clang/lib/Sema/SemaAMDGPU.cpp | 8 ++-- .../SemaCUDA/amdgpu-monitor-sleep-range.cu | 3 ++ .../builtins-amdgcn-error-gfx1250-param.cl | 2 + .../AMDGPU/AsmParser/AMDGPUAsmParser.cpp | 6 +-- llvm/lib/Target/AMDGPU/GCNFeatures.td | 11 +---- llvm/lib/Target/AMDGPU/SIInstrInfo.cpp | 3 +- .../AMDGPU/llvm.amdgcn.s.monitor.sleep.ll | 4 +- .../CodeGen/AMDGPU/monitor-sleep-gfx13.ll | 12 ----- .../AMDGPU/monitor-sleep-invalid-forever.ll | 4 ++ .../CodeGen/AMDGPU/monitor-sleep-invalid.ll | 4 ++ .../CodeGen/AMDGPU/monitor-sleep-invalid.mir | 44 +++++++++++++++++-- llvm/test/MC/AMDGPU/gfx13_asm_sopp.s | 4 +- .../MC/AMDGPU/monitor-sleep-duration-err.s | 2 + llvm/test/MC/AMDGPU/monitor-sleep-forever.s | 8 ++++ 14 files changed, 77 insertions(+), 38 deletions(-) delete mode 100644 llvm/test/CodeGen/AMDGPU/monitor-sleep-gfx13.ll diff --git a/clang/lib/Sema/SemaAMDGPU.cpp b/clang/lib/Sema/SemaAMDGPU.cpp index c27a33a3bd38c..04c73db6d9152 100644 --- a/clang/lib/Sema/SemaAMDGPU.cpp +++ b/clang/lib/Sema/SemaAMDGPU.cpp @@ -167,12 +167,10 @@ bool SemaAMDGPU::CheckAMDGCNBuiltinFunctionCall(const TargetInfo &TI, return false; } case AMDGPU::BI__builtin_amdgcn_s_monitor_sleep: { - bool Has7BitDuration = Builtin::evaluateRequiredTargetFeatures( - "monitor-sleep-7-bit-duration", CallerFeatureMap); + if (getLangOpts().CUDA && !getLangOpts().CUDAIsDevice) + return false; bool NoSleepForever = Builtin::evaluateRequiredTargetFeatures( "no-sleep-forever", CallerFeatureMap); - if (!Has7BitDuration && !NoSleepForever) - return false; Expr *ArgExpr = TheCall->getArg(0); if (ArgExpr->isInstantiationDependent()) return false; @@ -181,7 +179,7 @@ bool SemaAMDGPU::CheckAMDGCNBuiltinFunctionCall(const TargetInfo &TI, return true; // Bit 15 reaches Sema as the sign bit of the builtin's short argument. int Low = !NoSleepForever && Sleep.isNegative() ? INT16_MIN : 0; - int High = Has7BitDuration ? Low + 127 : INT16_MAX; + int High = Low + 127; return SemaRef.BuiltinConstantArgRange(TheCall, 0, Low, High); } case AMDGPU::BI__builtin_amdgcn_flat_prefetch: diff --git a/clang/test/SemaCUDA/amdgpu-monitor-sleep-range.cu b/clang/test/SemaCUDA/amdgpu-monitor-sleep-range.cu index 8517935eeff6a..793f9952ee21e 100644 --- a/clang/test/SemaCUDA/amdgpu-monitor-sleep-range.cu +++ b/clang/test/SemaCUDA/amdgpu-monitor-sleep-range.cu @@ -5,7 +5,10 @@ // RUN: %clang_cc1 -triple amdgpu12.51-amd-amdhsa -fcuda-is-device -x hip -fsyntax-only -verify=range,negative %s // RUN: %clang_cc1 -triple amdgpu12.50-amd-amdhsa -target-feature -no-sleep-forever -fcuda-is-device -x hip -fsyntax-only -verify=range,negative %s // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -aux-triple amdgpu12.50-amd-amdhsa -x hip -DNO_SLEEP_FOREVER -fsyntax-only -verify=host %s +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -aux-triple amdgpu13-amd-amdhsa -x hip -DNO_SLEEP_FOREVER -fsyntax-only -verify=host %s // RUN: %clang_cc1 -triple amdgpu12.5-amd-amdhsa -fcuda-is-device -x hip -fsyntax-only -verify=range,negative %s +// RUN: %clang_cc1 -triple amdgpu13-amd-amdhsa -fcuda-is-device -x hip -fsyntax-only -verify=range,negative %s +// RUN: %clang_cc1 -triple amdgpu13.10-amd-amdhsa -fcuda-is-device -x hip -fsyntax-only -verify=range,negative %s // host-no-diagnostics #include "Inputs/cuda.h" diff --git a/clang/test/SemaOpenCL/builtins-amdgcn-error-gfx1250-param.cl b/clang/test/SemaOpenCL/builtins-amdgcn-error-gfx1250-param.cl index b8bba4e20df76..c0437782194c4 100644 --- a/clang/test/SemaOpenCL/builtins-amdgcn-error-gfx1250-param.cl +++ b/clang/test/SemaOpenCL/builtins-amdgcn-error-gfx1250-param.cl @@ -2,6 +2,8 @@ // REQUIRES: amdgpu-registered-target // RUN: %clang_cc1 -cl-std=CL2.0 -triple amdgpu12.50-- -verify -S -o - %s // RUN: %clang_cc1 -cl-std=CL2.0 -triple amdgpu12.50s-amd-amdhsa -verify -S -o - %s +// RUN: %clang_cc1 -cl-std=CL2.0 -triple amdgpu13-amd-amdhsa -verify -S -o - %s +// RUN: %clang_cc1 -cl-std=CL2.0 -triple amdgpu13.10-amd-amdhsa -verify -S -o - %s typedef unsigned int uint; typedef unsigned short int ushort; diff --git a/llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp b/llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp index 5b58a3261774d..2533fd239e299 100644 --- a/llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp +++ b/llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp @@ -5559,7 +5559,8 @@ bool AMDGPUAsmParser::validateWMMA(const MCInst &Inst, bool AMDGPUAsmParser::validateMonitorSleep(const MCInst &Inst, const OperandVector &Operands) { unsigned Opc = Inst.getOpcode(); - if (Opc != AMDGPU::S_MONITOR_SLEEP_gfx12) + if (Opc != AMDGPU::S_MONITOR_SLEEP_gfx12 && + Opc != AMDGPU::S_MONITOR_SLEEP_gfx13) return true; int ImmIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::simm16); @@ -5572,8 +5573,7 @@ bool AMDGPUAsmParser::validateMonitorSleep(const MCInst &Inst, "sleep forever is unsupported on the target"); return false; } - if (getSTI().hasFeature(AMDGPU::FeatureMonitorSleep7BitDuration) && - (Sleep < INT16_MIN || Sleep > UINT16_MAX || (Sleep & 0x7fff) > 127)) { + if (Sleep < INT16_MIN || Sleep > UINT16_MAX || (Sleep & 0x7fff) > 127) { Error(getOperandLoc(Operands, ImmIdx), "sleep duration must be in range [0, 127] on this target"); return false; diff --git a/llvm/lib/Target/AMDGPU/GCNFeatures.td b/llvm/lib/Target/AMDGPU/GCNFeatures.td index 9e38e4ae516ba..bbf26837cba24 100644 --- a/llvm/lib/Target/AMDGPU/GCNFeatures.td +++ b/llvm/lib/Target/AMDGPU/GCNFeatures.td @@ -1695,12 +1695,6 @@ defm NeedsAligned2addrDS : AMDGPUSubtargetFeature<"needs-aligned-2addr-lds", /*GenPredicate=*/1 >; -defm MonitorSleep7BitDuration : AMDGPUSubtargetFeature< - "monitor-sleep-7-bit-duration", - "s_monitor_sleep uses a 7-bit duration", - /*GenPredicate=*/0 ->; - defm NoSleepForever : AMDGPUSubtargetFeature<"no-sleep-forever", "s_monitor_sleep should not use sleep_forever", /*GenPredicate=*/1 @@ -2509,7 +2503,6 @@ def FeatureISAVersion12 : FeatureSet< def FeatureISAVersion12_50_Common : FeatureSet< [FeatureGFX12, - FeatureMonitorSleep7BitDuration, FeatureGFX1250Insts, FeatureBackOffBarrier, FeatureRequiresAlignedVGPRs, @@ -2862,8 +2855,8 @@ def AMDGPUFrontendVisibleFeatures { FeatureGFX940Insts, FeatureGFX950Insts, FeatureGFX9Insts, FeatureGWS, FeatureImageInsts, FeatureLdsBarrierArriveAtomic, FeatureLerpInst, FeatureLoadMonitorInsts, FeatureMAIInsts, FeatureMcastLoadInsts, - FeatureMonitorSleep7BitDuration, FeatureMqsadInsts, FeatureMqsadPkInsts, - FeatureMsadInsts, FeatureNoSleepForever, + FeatureMqsadInsts, FeatureMqsadPkInsts, FeatureMsadInsts, + FeatureNoSleepForever, FeatureOCPFP8ConversionInsts, FeaturePermlane16Insts, FeaturePermlane16Swap, FeaturePermlane32Swap, FeaturePkAddMinMaxInsts, FeaturePrngInst, FeatureQsadInsts, FeatureSMemRealTime, FeatureSMemTimeInst, diff --git a/llvm/lib/Target/AMDGPU/SIInstrInfo.cpp b/llvm/lib/Target/AMDGPU/SIInstrInfo.cpp index a93372c471410..16db2f86c107a 100644 --- a/llvm/lib/Target/AMDGPU/SIInstrInfo.cpp +++ b/llvm/lib/Target/AMDGPU/SIInstrInfo.cpp @@ -5618,8 +5618,7 @@ bool SIInstrInfo::verifyInstruction(const MachineInstr &MI, ErrInfo = "sleep forever is unsupported on this target"; return false; } - if (ST.hasMonitorSleep7BitDuration() && - ((!isInt<16>(Value) && !isUInt<16>(Value)) || (Value & 0x7fff) > 127)) { + if ((!isInt<16>(Value) && !isUInt<16>(Value)) || (Value & 0x7fff) > 127) { ErrInfo = "sleep duration must be in range [0, 127] on this target"; return false; } diff --git a/llvm/test/CodeGen/AMDGPU/llvm.amdgcn.s.monitor.sleep.ll b/llvm/test/CodeGen/AMDGPU/llvm.amdgcn.s.monitor.sleep.ll index c9119129b9ff0..1ff7978ea326b 100644 --- a/llvm/test/CodeGen/AMDGPU/llvm.amdgcn.s.monitor.sleep.ll +++ b/llvm/test/CodeGen/AMDGPU/llvm.amdgcn.s.monitor.sleep.ll @@ -11,12 +11,12 @@ ; RUN: llc -global-isel=0 -mtriple=amdgpu13 < %s | FileCheck --check-prefixes=GCN,GFX1251 %s ; RUN: llc -global-isel=1 -global-isel-abort=1 -mtriple=amdgpu13 < %s | FileCheck --check-prefixes=GCN,GFX1251 %s +; RUN: llc -global-isel=0 -mtriple=amdgpu13.10 < %s | FileCheck --check-prefixes=GCN,GFX1251 %s +; RUN: llc -global-isel=1 -global-isel-abort=1 -mtriple=amdgpu13.10 < %s | FileCheck --check-prefixes=GCN,GFX1251 %s ; RUN: llc -global-isel=0 -mtriple=amdgpu12.5 < %s | FileCheck --check-prefixes=GCN,GFX1251 %s ; RUN: llc -global-isel=1 -global-isel-abort=1 -mtriple=amdgpu12.5 < %s | FileCheck --check-prefixes=GCN,GFX1251 %s -declare void @llvm.amdgcn.s.monitor.sleep(i16) - ; GCN-LABEL: {{^}}test_monitor_sleep_1: ; GCN: s_monitor_sleep 1 define amdgpu_ps void @test_monitor_sleep_1() { diff --git a/llvm/test/CodeGen/AMDGPU/monitor-sleep-gfx13.ll b/llvm/test/CodeGen/AMDGPU/monitor-sleep-gfx13.ll deleted file mode 100644 index 413aee985c302..0000000000000 --- a/llvm/test/CodeGen/AMDGPU/monitor-sleep-gfx13.ll +++ /dev/null @@ -1,12 +0,0 @@ -; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6 -; RUN: llc -global-isel=0 -mtriple=amdgpu13 < %s | FileCheck %s -; RUN: llc -global-isel=1 -mtriple=amdgpu13 < %s | FileCheck %s - -define amdgpu_ps void @sleep_duration() { -; CHECK-LABEL: sleep_duration: -; CHECK: ; %bb.0: -; CHECK-NEXT: s_monitor_sleep 0x1234 -; CHECK-NEXT: s_endpgm - call void @llvm.amdgcn.s.monitor.sleep(i16 4660) - ret void -} diff --git a/llvm/test/CodeGen/AMDGPU/monitor-sleep-invalid-forever.ll b/llvm/test/CodeGen/AMDGPU/monitor-sleep-invalid-forever.ll index 9d583e08953ea..ab1ecfbafb530 100644 --- a/llvm/test/CodeGen/AMDGPU/monitor-sleep-invalid-forever.ll +++ b/llvm/test/CodeGen/AMDGPU/monitor-sleep-invalid-forever.ll @@ -1,6 +1,10 @@ ; NOTE: Do not autogenerate. Check diagnostics for reserved duration bits with sleep forever enabled. ; RUN: not llc -global-isel=0 -verify-machineinstrs=0 -mtriple=amdgpu12.51 < %s 2>&1 | FileCheck %s ; RUN: not llc -global-isel=1 -verify-machineinstrs=0 -mtriple=amdgpu12.51 < %s 2>&1 | FileCheck %s +; RUN: not llc -global-isel=0 -verify-machineinstrs=0 -mtriple=amdgpu13 < %s 2>&1 | FileCheck %s +; RUN: not llc -global-isel=1 -verify-machineinstrs=0 -mtriple=amdgpu13 < %s 2>&1 | FileCheck %s +; RUN: not llc -global-isel=0 -verify-machineinstrs=0 -mtriple=amdgpu13.10 < %s 2>&1 | FileCheck %s +; RUN: not llc -global-isel=1 -verify-machineinstrs=0 -mtriple=amdgpu13.10 < %s 2>&1 | FileCheck %s ; CHECK: error: Illegal instruction detected: sleep duration must be in range [0, 127] on this target ; CHECK: error: Illegal instruction detected: sleep duration must be in range [0, 127] on this target diff --git a/llvm/test/CodeGen/AMDGPU/monitor-sleep-invalid.ll b/llvm/test/CodeGen/AMDGPU/monitor-sleep-invalid.ll index 34cd15a760115..5d418a196fc66 100644 --- a/llvm/test/CodeGen/AMDGPU/monitor-sleep-invalid.ll +++ b/llvm/test/CodeGen/AMDGPU/monitor-sleep-invalid.ll @@ -8,6 +8,10 @@ ; RUN: not llc -global-isel=1 -verify-machineinstrs=0 -mtriple=amdgpu12.51 < %s 2>&1 | FileCheck %s ; RUN: not llc -global-isel=0 -verify-machineinstrs=0 -mtriple=amdgpu12.5 < %s 2>&1 | FileCheck %s ; RUN: not llc -global-isel=1 -verify-machineinstrs=0 -mtriple=amdgpu12.5 < %s 2>&1 | FileCheck %s +; RUN: not llc -global-isel=0 -verify-machineinstrs=0 -mtriple=amdgpu13 < %s 2>&1 | FileCheck %s +; RUN: not llc -global-isel=1 -verify-machineinstrs=0 -mtriple=amdgpu13 < %s 2>&1 | FileCheck %s +; RUN: not llc -global-isel=0 -verify-machineinstrs=0 -mtriple=amdgpu13.10 < %s 2>&1 | FileCheck %s +; RUN: not llc -global-isel=1 -verify-machineinstrs=0 -mtriple=amdgpu13.10 < %s 2>&1 | FileCheck %s ; CHECK: error: Illegal instruction detected: sleep duration must be in range [0, 127] on this target ; CHECK: error: Illegal instruction detected: sleep duration must be in range [0, 127] on this target diff --git a/llvm/test/CodeGen/AMDGPU/monitor-sleep-invalid.mir b/llvm/test/CodeGen/AMDGPU/monitor-sleep-invalid.mir index 8f2acb416713c..ce4d9ac0bb4ef 100644 --- a/llvm/test/CodeGen/AMDGPU/monitor-sleep-invalid.mir +++ b/llvm/test/CodeGen/AMDGPU/monitor-sleep-invalid.mir @@ -6,6 +6,9 @@ # RUN: llc -mtriple=amdgpu13 -run-pass=none -verify-machineinstrs -filetype=null %t/gfx13.mir # RUN: not --crash llc -mtriple=amdgpu12.51 -run-pass=none -verify-machineinstrs -filetype=null %t/duration.mir 2>&1 | FileCheck %s --check-prefix=DURATION # RUN: not --crash llc -mtriple=amdgpu12.50 -run-pass=none -verify-machineinstrs -filetype=null %t/forever.mir 2>&1 | FileCheck %s --check-prefix=FOREVER +# RUN: not --crash llc -mtriple=amdgpu13 -run-pass=none -verify-machineinstrs -filetype=null %t/gfx13-duration.mir 2>&1 | FileCheck %s --check-prefix=GFX13-DURATION +# RUN: llc -mtriple=amdgpu13.10 -run-pass=none -verify-machineinstrs -filetype=null %t/gfx13.mir +# RUN: not --crash llc -mtriple=amdgpu13.10 -run-pass=none -verify-machineinstrs -filetype=null %t/gfx13-duration.mir 2>&1 | FileCheck %s --check-prefix=GFX13-DURATION # DURATION: Bad machine code: sleep duration must be in range [0, 127] on this target # DURATION: S_MONITOR_SLEEP 200 @@ -20,6 +23,19 @@ # FOREVER: Bad machine code: sleep forever is unsupported on this target # FOREVER: S_MONITOR_SLEEP_gfx12 32768 +# GFX13-DURATION: Bad machine code: sleep duration must be in range [0, 127] on this target +# GFX13-DURATION: S_MONITOR_SLEEP 128 +# GFX13-DURATION: Bad machine code: sleep duration must be in range [0, 127] on this target +# GFX13-DURATION: S_MONITOR_SLEEP 65536 +# GFX13-DURATION: Bad machine code: sleep duration must be in range [0, 127] on this target +# GFX13-DURATION: S_MONITOR_SLEEP_gfx13 200 +# GFX13-DURATION: Bad machine code: sleep duration must be in range [0, 127] on this target +# GFX13-DURATION: S_MONITOR_SLEEP_gfx13 65536 +# GFX13-DURATION: Bad machine code: sleep duration must be in range [0, 127] on this target +# GFX13-DURATION: S_MONITOR_SLEEP_gfx13 32896 +# GFX13-DURATION: Bad machine code: sleep duration must be in range [0, 127] on this target +# GFX13-DURATION: S_MONITOR_SLEEP_gfx13 -1 + #--- duration.mir --- name: sleep_200 @@ -68,10 +84,32 @@ body: | #--- gfx13.mir --- -name: wider_duration +name: valid_duration_and_forever body: | bb.0: - S_MONITOR_SLEEP 4660 - S_MONITOR_SLEEP_gfx13 4660 + S_MONITOR_SLEEP 0 + S_MONITOR_SLEEP 127 + S_MONITOR_SLEEP 32768 + S_MONITOR_SLEEP -32641 + S_MONITOR_SLEEP_gfx13 0 + S_MONITOR_SLEEP_gfx13 127 + S_MONITOR_SLEEP_gfx13 32768 + S_MONITOR_SLEEP_gfx13 32895 + S_MONITOR_SLEEP_gfx13 -32768 + S_MONITOR_SLEEP_gfx13 -32641 + S_ENDPGM 0 +... + +#--- gfx13-duration.mir +--- +name: invalid_duration +body: | + bb.0: + S_MONITOR_SLEEP 128 + S_MONITOR_SLEEP 65536 + S_MONITOR_SLEEP_gfx13 200 + S_MONITOR_SLEEP_gfx13 65536 + S_MONITOR_SLEEP_gfx13 32896 + S_MONITOR_SLEEP_gfx13 -1 S_ENDPGM 0 ... diff --git a/llvm/test/MC/AMDGPU/gfx13_asm_sopp.s b/llvm/test/MC/AMDGPU/gfx13_asm_sopp.s index 8d6bf2e1bedfc..df094889cbff1 100644 --- a/llvm/test/MC/AMDGPU/gfx13_asm_sopp.s +++ b/llvm/test/MC/AMDGPU/gfx13_asm_sopp.s @@ -185,8 +185,8 @@ s_barrier_wait 0x1234 s_monitor_sleep 0 // GFX13: s_monitor_sleep 0 ; encoding: [0x00,0x00,0xac,0xbf] -s_monitor_sleep 0x1234 -// GFX13: s_monitor_sleep 0x1234 ; encoding: [0x34,0x12,0xac,0xbf] +s_monitor_sleep 0x7f +// GFX13: s_monitor_sleep 0x7f ; encoding: [0x7f,0x00,0xac,0xbf] s_delay_alu 0 // GFX13: s_delay_alu 0 ; encoding: [0x00,0x00,0xae,0xbf] diff --git a/llvm/test/MC/AMDGPU/monitor-sleep-duration-err.s b/llvm/test/MC/AMDGPU/monitor-sleep-duration-err.s index 5aa4d39343c31..0186022c9f049 100644 --- a/llvm/test/MC/AMDGPU/monitor-sleep-duration-err.s +++ b/llvm/test/MC/AMDGPU/monitor-sleep-duration-err.s @@ -5,6 +5,8 @@ // RUN: not llvm-mc -triple=amdgpu12.50s -filetype=null %s 2>&1 | FileCheck --check-prefixes=ERR,NO-FOREVER --implicit-check-not=error: -strict-whitespace %s // RUN: not llvm-mc -triple=amdgpu12.51 -filetype=null %s 2>&1 | FileCheck --check-prefixes=ERR,FOREVER --implicit-check-not=error: -strict-whitespace %s // RUN: not llvm-mc -triple=amdgpu12.50 -mattr=-no-sleep-forever -filetype=null %s 2>&1 | FileCheck --check-prefixes=ERR,FOREVER --implicit-check-not=error: -strict-whitespace %s +// RUN: not llvm-mc -triple=amdgpu13 -filetype=null %s 2>&1 | FileCheck --check-prefixes=ERR,FOREVER --implicit-check-not=error: -strict-whitespace %s +// RUN: not llvm-mc -triple=amdgpu13.10 -filetype=null %s 2>&1 | FileCheck --check-prefixes=ERR,FOREVER --implicit-check-not=error: -strict-whitespace %s s_monitor_sleep -1 // FOREVER: :[[@LINE-1]]:17: error: sleep duration must be in range [0, 127] on this target diff --git a/llvm/test/MC/AMDGPU/monitor-sleep-forever.s b/llvm/test/MC/AMDGPU/monitor-sleep-forever.s index c2f52a8af7b10..e47f940834f19 100644 --- a/llvm/test/MC/AMDGPU/monitor-sleep-forever.s +++ b/llvm/test/MC/AMDGPU/monitor-sleep-forever.s @@ -4,17 +4,25 @@ // RUN: llvm-mc -triple=amdgpu12.50s -mattr=-no-sleep-forever -show-encoding %s | FileCheck %s // RUN: llvm-mc -triple=amdgpu12.5 -show-encoding %s | FileCheck %s +// RUN: llvm-mc -triple=amdgpu13 -show-encoding %s | FileCheck --check-prefix=GFX13 %s +// RUN: llvm-mc -triple=amdgpu13.10 -show-encoding %s | FileCheck --check-prefix=GFX13 %s s_monitor_sleep 0x8000 // CHECK: s_monitor_sleep 0x8000 ; encoding: [0x00,0x80,0x84,0xbf] +// GFX13: s_monitor_sleep 0x8000 ; encoding: [0x00,0x80,0xac,0xbf] s_monitor_sleep 0x807f // CHECK: s_monitor_sleep 0x807f ; encoding: [0x7f,0x80,0x84,0xbf] +// GFX13: s_monitor_sleep 0x807f ; encoding: [0x7f,0x80,0xac,0xbf] s_monitor_sleep -32768 // CHECK: s_monitor_sleep 0xffff8000 ; encoding: [0x00,0x80,0x84,0xbf] +// GFX13: s_monitor_sleep 0xffff8000 ; encoding: [0x00,0x80,0xac,0xbf] s_monitor_sleep -32641 // CHECK: s_monitor_sleep 0xffff807f ; encoding: [0x7f,0x80,0x84,0xbf] +// GFX13: s_monitor_sleep 0xffff807f ; encoding: [0x7f,0x80,0xac,0xbf] s_monitor_sleep 0xffff8000 // CHECK: s_monitor_sleep 0xffff8000 ; encoding: [0x00,0x80,0x84,0xbf] +// GFX13: s_monitor_sleep 0xffff8000 ; encoding: [0x00,0x80,0xac,0xbf] s_monitor_sleep 0xffff807f // CHECK: s_monitor_sleep 0xffff807f ; encoding: [0x7f,0x80,0x84,0xbf] +// GFX13: s_monitor_sleep 0xffff807f ; encoding: [0x7f,0x80,0xac,0xbf] >From ae5e2caf8c567b639e5f4af8d6d587eeaf0283dc Mon Sep 17 00:00:00 2001 From: Yaxun Liu <[email protected]> Date: Fri, 13 Nov 2026 04:55:19 +0000 Subject: [PATCH 5/6] [AMDGPU] Move monitor-sleep diagnostics to verifier tests Keep monitor-sleep operand diagnostics in the MachineVerifier suite, independent of the check performed during instruction emission. Preserve reserved sleep-forever cases and the supported target variants. AI assisted. --- llvm/lib/Target/AMDGPU/SIInstrInfo.cpp | 1 + .../AMDGPU/monitor-sleep-invalid-forever.ll | 20 --- .../CodeGen/AMDGPU/monitor-sleep-invalid.ll | 27 --- .../CodeGen/AMDGPU/monitor-sleep-invalid.mir | 115 ------------- .../AMDGPU/monitor-sleep-invalid.mir | 158 ++++++++++++++++++ 5 files changed, 159 insertions(+), 162 deletions(-) delete mode 100644 llvm/test/CodeGen/AMDGPU/monitor-sleep-invalid-forever.ll delete mode 100644 llvm/test/CodeGen/AMDGPU/monitor-sleep-invalid.ll delete mode 100644 llvm/test/CodeGen/AMDGPU/monitor-sleep-invalid.mir create mode 100644 llvm/test/MachineVerifier/AMDGPU/monitor-sleep-invalid.mir diff --git a/llvm/lib/Target/AMDGPU/SIInstrInfo.cpp b/llvm/lib/Target/AMDGPU/SIInstrInfo.cpp index 16db2f86c107a..e49218ec6bdbd 100644 --- a/llvm/lib/Target/AMDGPU/SIInstrInfo.cpp +++ b/llvm/lib/Target/AMDGPU/SIInstrInfo.cpp @@ -5618,6 +5618,7 @@ bool SIInstrInfo::verifyInstruction(const MachineInstr &MI, ErrInfo = "sleep forever is unsupported on this target"; return false; } + if ((!isInt<16>(Value) && !isUInt<16>(Value)) || (Value & 0x7fff) > 127) { ErrInfo = "sleep duration must be in range [0, 127] on this target"; return false; diff --git a/llvm/test/CodeGen/AMDGPU/monitor-sleep-invalid-forever.ll b/llvm/test/CodeGen/AMDGPU/monitor-sleep-invalid-forever.ll deleted file mode 100644 index ab1ecfbafb530..0000000000000 --- a/llvm/test/CodeGen/AMDGPU/monitor-sleep-invalid-forever.ll +++ /dev/null @@ -1,20 +0,0 @@ -; NOTE: Do not autogenerate. Check diagnostics for reserved duration bits with sleep forever enabled. -; RUN: not llc -global-isel=0 -verify-machineinstrs=0 -mtriple=amdgpu12.51 < %s 2>&1 | FileCheck %s -; RUN: not llc -global-isel=1 -verify-machineinstrs=0 -mtriple=amdgpu12.51 < %s 2>&1 | FileCheck %s -; RUN: not llc -global-isel=0 -verify-machineinstrs=0 -mtriple=amdgpu13 < %s 2>&1 | FileCheck %s -; RUN: not llc -global-isel=1 -verify-machineinstrs=0 -mtriple=amdgpu13 < %s 2>&1 | FileCheck %s -; RUN: not llc -global-isel=0 -verify-machineinstrs=0 -mtriple=amdgpu13.10 < %s 2>&1 | FileCheck %s -; RUN: not llc -global-isel=1 -verify-machineinstrs=0 -mtriple=amdgpu13.10 < %s 2>&1 | FileCheck %s - -; CHECK: error: Illegal instruction detected: sleep duration must be in range [0, 127] on this target -; CHECK: error: Illegal instruction detected: sleep duration must be in range [0, 127] on this target - -define amdgpu_ps void @sleep_forever_128() { - call void @llvm.amdgcn.s.monitor.sleep(i16 -32640) - ret void -} - -define amdgpu_ps void @sleep_forever_reserved() { - call void @llvm.amdgcn.s.monitor.sleep(i16 -1) - ret void -} diff --git a/llvm/test/CodeGen/AMDGPU/monitor-sleep-invalid.ll b/llvm/test/CodeGen/AMDGPU/monitor-sleep-invalid.ll deleted file mode 100644 index 5d418a196fc66..0000000000000 --- a/llvm/test/CodeGen/AMDGPU/monitor-sleep-invalid.ll +++ /dev/null @@ -1,27 +0,0 @@ -; NOTE: Do not autogenerate. Check diagnostics rather than assembly. -; Disable MachineVerifier to exercise the check during code emission. -; RUN: not llc -global-isel=0 -verify-machineinstrs=0 -mtriple=amdgpu12.50 < %s 2>&1 | FileCheck %s -; RUN: not llc -global-isel=1 -verify-machineinstrs=0 -mtriple=amdgpu12.50 < %s 2>&1 | FileCheck %s -; RUN: not llc -global-isel=0 -verify-machineinstrs=0 -mtriple=amdgpu12.50s < %s 2>&1 | FileCheck %s -; RUN: not llc -global-isel=1 -verify-machineinstrs=0 -mtriple=amdgpu12.50s < %s 2>&1 | FileCheck %s -; RUN: not llc -global-isel=0 -verify-machineinstrs=0 -mtriple=amdgpu12.51 < %s 2>&1 | FileCheck %s -; RUN: not llc -global-isel=1 -verify-machineinstrs=0 -mtriple=amdgpu12.51 < %s 2>&1 | FileCheck %s -; RUN: not llc -global-isel=0 -verify-machineinstrs=0 -mtriple=amdgpu12.5 < %s 2>&1 | FileCheck %s -; RUN: not llc -global-isel=1 -verify-machineinstrs=0 -mtriple=amdgpu12.5 < %s 2>&1 | FileCheck %s -; RUN: not llc -global-isel=0 -verify-machineinstrs=0 -mtriple=amdgpu13 < %s 2>&1 | FileCheck %s -; RUN: not llc -global-isel=1 -verify-machineinstrs=0 -mtriple=amdgpu13 < %s 2>&1 | FileCheck %s -; RUN: not llc -global-isel=0 -verify-machineinstrs=0 -mtriple=amdgpu13.10 < %s 2>&1 | FileCheck %s -; RUN: not llc -global-isel=1 -verify-machineinstrs=0 -mtriple=amdgpu13.10 < %s 2>&1 | FileCheck %s - -; CHECK: error: Illegal instruction detected: sleep duration must be in range [0, 127] on this target -; CHECK: error: Illegal instruction detected: sleep duration must be in range [0, 127] on this target - -define amdgpu_ps void @sleep_128() { - call void @llvm.amdgcn.s.monitor.sleep(i16 128) - ret void -} - -define amdgpu_ps void @sleep_200() { - call void @llvm.amdgcn.s.monitor.sleep(i16 200) - ret void -} diff --git a/llvm/test/CodeGen/AMDGPU/monitor-sleep-invalid.mir b/llvm/test/CodeGen/AMDGPU/monitor-sleep-invalid.mir deleted file mode 100644 index ce4d9ac0bb4ef..0000000000000 --- a/llvm/test/CodeGen/AMDGPU/monitor-sleep-invalid.mir +++ /dev/null @@ -1,115 +0,0 @@ -# NOTE: Do not autogenerate. Check the target's MachineVerifier diagnostics. -# RUN: split-file %s %t -# RUN: llc -mtriple=amdgpu12.50 -run-pass=none -verify-machineinstrs -filetype=null %t/valid-duration.mir -# RUN: llc -mtriple=amdgpu12.51 -run-pass=none -verify-machineinstrs -filetype=null %t/valid-duration.mir -# RUN: llc -mtriple=amdgpu12.51 -run-pass=none -verify-machineinstrs -filetype=null %t/valid-forever.mir -# RUN: llc -mtriple=amdgpu13 -run-pass=none -verify-machineinstrs -filetype=null %t/gfx13.mir -# RUN: not --crash llc -mtriple=amdgpu12.51 -run-pass=none -verify-machineinstrs -filetype=null %t/duration.mir 2>&1 | FileCheck %s --check-prefix=DURATION -# RUN: not --crash llc -mtriple=amdgpu12.50 -run-pass=none -verify-machineinstrs -filetype=null %t/forever.mir 2>&1 | FileCheck %s --check-prefix=FOREVER -# RUN: not --crash llc -mtriple=amdgpu13 -run-pass=none -verify-machineinstrs -filetype=null %t/gfx13-duration.mir 2>&1 | FileCheck %s --check-prefix=GFX13-DURATION -# RUN: llc -mtriple=amdgpu13.10 -run-pass=none -verify-machineinstrs -filetype=null %t/gfx13.mir -# RUN: not --crash llc -mtriple=amdgpu13.10 -run-pass=none -verify-machineinstrs -filetype=null %t/gfx13-duration.mir 2>&1 | FileCheck %s --check-prefix=GFX13-DURATION - -# DURATION: Bad machine code: sleep duration must be in range [0, 127] on this target -# DURATION: S_MONITOR_SLEEP 200 -# DURATION: Bad machine code: sleep duration must be in range [0, 127] on this target -# DURATION: S_MONITOR_SLEEP 65536 -# DURATION: Bad machine code: sleep duration must be in range [0, 127] on this target -# DURATION: S_MONITOR_SLEEP_gfx12 200 -# DURATION: Bad machine code: sleep duration must be in range [0, 127] on this target -# DURATION: S_MONITOR_SLEEP_gfx12 65536 -# FOREVER: Bad machine code: sleep forever is unsupported on this target -# FOREVER: S_MONITOR_SLEEP 32768 -# FOREVER: Bad machine code: sleep forever is unsupported on this target -# FOREVER: S_MONITOR_SLEEP_gfx12 32768 - -# GFX13-DURATION: Bad machine code: sleep duration must be in range [0, 127] on this target -# GFX13-DURATION: S_MONITOR_SLEEP 128 -# GFX13-DURATION: Bad machine code: sleep duration must be in range [0, 127] on this target -# GFX13-DURATION: S_MONITOR_SLEEP 65536 -# GFX13-DURATION: Bad machine code: sleep duration must be in range [0, 127] on this target -# GFX13-DURATION: S_MONITOR_SLEEP_gfx13 200 -# GFX13-DURATION: Bad machine code: sleep duration must be in range [0, 127] on this target -# GFX13-DURATION: S_MONITOR_SLEEP_gfx13 65536 -# GFX13-DURATION: Bad machine code: sleep duration must be in range [0, 127] on this target -# GFX13-DURATION: S_MONITOR_SLEEP_gfx13 32896 -# GFX13-DURATION: Bad machine code: sleep duration must be in range [0, 127] on this target -# GFX13-DURATION: S_MONITOR_SLEEP_gfx13 -1 - -#--- duration.mir ---- -name: sleep_200 -body: | - bb.0: - S_MONITOR_SLEEP 200 - S_MONITOR_SLEEP 65536 - S_MONITOR_SLEEP_gfx12 200 - S_MONITOR_SLEEP_gfx12 65536 - S_ENDPGM 0 -... - -#--- forever.mir ---- -name: sleep_forever -body: | - bb.0: - S_MONITOR_SLEEP 32768 - S_MONITOR_SLEEP_gfx12 32768 - S_ENDPGM 0 -... - -#--- valid-duration.mir ---- -name: valid_duration -body: | - bb.0: - S_MONITOR_SLEEP 0 - S_MONITOR_SLEEP 127 - S_MONITOR_SLEEP_gfx12 0 - S_MONITOR_SLEEP_gfx12 127 - S_ENDPGM 0 -... - -#--- valid-forever.mir ---- -name: valid_forever -body: | - bb.0: - S_MONITOR_SLEEP_gfx12 32768 - S_MONITOR_SLEEP_gfx12 32895 - S_MONITOR_SLEEP_gfx12 -32768 - S_MONITOR_SLEEP_gfx12 -32641 - S_ENDPGM 0 -... - -#--- gfx13.mir ---- -name: valid_duration_and_forever -body: | - bb.0: - S_MONITOR_SLEEP 0 - S_MONITOR_SLEEP 127 - S_MONITOR_SLEEP 32768 - S_MONITOR_SLEEP -32641 - S_MONITOR_SLEEP_gfx13 0 - S_MONITOR_SLEEP_gfx13 127 - S_MONITOR_SLEEP_gfx13 32768 - S_MONITOR_SLEEP_gfx13 32895 - S_MONITOR_SLEEP_gfx13 -32768 - S_MONITOR_SLEEP_gfx13 -32641 - S_ENDPGM 0 -... - -#--- gfx13-duration.mir ---- -name: invalid_duration -body: | - bb.0: - S_MONITOR_SLEEP 128 - S_MONITOR_SLEEP 65536 - S_MONITOR_SLEEP_gfx13 200 - S_MONITOR_SLEEP_gfx13 65536 - S_MONITOR_SLEEP_gfx13 32896 - S_MONITOR_SLEEP_gfx13 -1 - S_ENDPGM 0 -... diff --git a/llvm/test/MachineVerifier/AMDGPU/monitor-sleep-invalid.mir b/llvm/test/MachineVerifier/AMDGPU/monitor-sleep-invalid.mir new file mode 100644 index 0000000000000..3f945dbb9133b --- /dev/null +++ b/llvm/test/MachineVerifier/AMDGPU/monitor-sleep-invalid.mir @@ -0,0 +1,158 @@ +# NOTE: Do not autogenerate. Check the target's MachineVerifier diagnostics. +# RUN: split-file %s %t +# RUN: llc -mtriple=amdgpu12.50 -run-pass=none -filetype=null %t/valid-duration.mir +# RUN: llc -mtriple=amdgpu12.51 -run-pass=none -filetype=null %t/valid-duration.mir +# RUN: llc -mtriple=amdgpu12.50s -run-pass=none -filetype=null %t/valid-duration.mir +# RUN: llc -mtriple=amdgpu12.5 -run-pass=none -filetype=null %t/valid-duration.mir +# RUN: llc -mtriple=amdgpu12.51 -run-pass=none -filetype=null %t/valid-forever.mir +# RUN: llc -mtriple=amdgpu13 -run-pass=none -filetype=null %t/gfx13.mir +# RUN: not --crash llc -mtriple=amdgpu12.51 -run-pass=none -filetype=null %t/duration.mir 2>&1 | FileCheck %s --check-prefix=DURATION +# RUN: not --crash llc -mtriple=amdgpu12.50 -run-pass=none -filetype=null %t/duration.mir 2>&1 | FileCheck %s --check-prefix=DURATION +# RUN: not --crash llc -mtriple=amdgpu12.50s -run-pass=none -filetype=null %t/duration.mir 2>&1 | FileCheck %s --check-prefix=DURATION +# RUN: not --crash llc -mtriple=amdgpu12.5 -run-pass=none -filetype=null %t/duration.mir 2>&1 | FileCheck %s --check-prefix=DURATION +# RUN: not --crash llc -mtriple=amdgpu12.50 -run-pass=none -filetype=null %t/forever.mir 2>&1 | FileCheck %s --check-prefix=FOREVER +# RUN: not --crash llc -mtriple=amdgpu12.50s -run-pass=none -filetype=null %t/forever.mir 2>&1 | FileCheck %s --check-prefix=FOREVER +# RUN: not --crash llc -mtriple=amdgpu12.51 -run-pass=none -filetype=null %t/reserved-forever.mir 2>&1 | FileCheck %s --check-prefix=RESERVED-FOREVER +# RUN: not --crash llc -mtriple=amdgpu13 -run-pass=none -filetype=null %t/gfx13-duration.mir 2>&1 | FileCheck %s --check-prefix=GFX13-DURATION +# RUN: llc -mtriple=amdgpu13.10 -run-pass=none -filetype=null %t/gfx13.mir +# RUN: not --crash llc -mtriple=amdgpu13.10 -run-pass=none -filetype=null %t/gfx13-duration.mir 2>&1 | FileCheck %s --check-prefix=GFX13-DURATION + +# DURATION: Bad machine code: sleep duration must be in range [0, 127] on this target +# DURATION: S_MONITOR_SLEEP 128 +# DURATION: Bad machine code: sleep duration must be in range [0, 127] on this target +# DURATION: S_MONITOR_SLEEP 200 +# DURATION: Bad machine code: sleep duration must be in range [0, 127] on this target +# DURATION: S_MONITOR_SLEEP 65536 +# DURATION: Bad machine code: sleep duration must be in range [0, 127] on this target +# DURATION: S_MONITOR_SLEEP_gfx12 128 +# DURATION: Bad machine code: sleep duration must be in range [0, 127] on this target +# DURATION: S_MONITOR_SLEEP_gfx12 200 +# DURATION: Bad machine code: sleep duration must be in range [0, 127] on this target +# DURATION: S_MONITOR_SLEEP_gfx12 65536 +# FOREVER: Bad machine code: sleep forever is unsupported on this target +# FOREVER: S_MONITOR_SLEEP 32768 +# FOREVER: Bad machine code: sleep forever is unsupported on this target +# FOREVER: S_MONITOR_SLEEP_gfx12 32768 + +# GFX13-DURATION: Bad machine code: sleep duration must be in range [0, 127] on this target +# GFX13-DURATION: S_MONITOR_SLEEP 128 +# GFX13-DURATION: Bad machine code: sleep duration must be in range [0, 127] on this target +# GFX13-DURATION: S_MONITOR_SLEEP 65536 +# GFX13-DURATION: Bad machine code: sleep duration must be in range [0, 127] on this target +# GFX13-DURATION: S_MONITOR_SLEEP -32640 +# GFX13-DURATION: Bad machine code: sleep duration must be in range [0, 127] on this target +# GFX13-DURATION: S_MONITOR_SLEEP -1 +# GFX13-DURATION: Bad machine code: sleep duration must be in range [0, 127] on this target +# GFX13-DURATION: S_MONITOR_SLEEP_gfx13 200 +# GFX13-DURATION: Bad machine code: sleep duration must be in range [0, 127] on this target +# GFX13-DURATION: S_MONITOR_SLEEP_gfx13 65536 +# GFX13-DURATION: Bad machine code: sleep duration must be in range [0, 127] on this target +# GFX13-DURATION: S_MONITOR_SLEEP_gfx13 32896 +# GFX13-DURATION: Bad machine code: sleep duration must be in range [0, 127] on this target +# GFX13-DURATION: S_MONITOR_SLEEP_gfx13 -32640 +# GFX13-DURATION: Bad machine code: sleep duration must be in range [0, 127] on this target +# GFX13-DURATION: S_MONITOR_SLEEP_gfx13 -1 + +# RESERVED-FOREVER: Bad machine code: sleep duration must be in range [0, 127] on this target +# RESERVED-FOREVER: S_MONITOR_SLEEP -32640 +# RESERVED-FOREVER: Bad machine code: sleep duration must be in range [0, 127] on this target +# RESERVED-FOREVER: S_MONITOR_SLEEP -1 +# RESERVED-FOREVER: Bad machine code: sleep duration must be in range [0, 127] on this target +# RESERVED-FOREVER: S_MONITOR_SLEEP_gfx12 -32640 +# RESERVED-FOREVER: Bad machine code: sleep duration must be in range [0, 127] on this target +# RESERVED-FOREVER: S_MONITOR_SLEEP_gfx12 -1 + +#--- duration.mir +--- +name: sleep_200 +body: | + bb.0: + S_MONITOR_SLEEP 128 + S_MONITOR_SLEEP 200 + S_MONITOR_SLEEP 65536 + S_MONITOR_SLEEP_gfx12 128 + S_MONITOR_SLEEP_gfx12 200 + S_MONITOR_SLEEP_gfx12 65536 + S_ENDPGM 0 +... + +#--- forever.mir +--- +name: sleep_forever +body: | + bb.0: + S_MONITOR_SLEEP 32768 + S_MONITOR_SLEEP_gfx12 32768 + S_ENDPGM 0 +... + +#--- valid-duration.mir +--- +name: valid_duration +body: | + bb.0: + S_MONITOR_SLEEP 0 + S_MONITOR_SLEEP 127 + S_MONITOR_SLEEP_gfx12 0 + S_MONITOR_SLEEP_gfx12 127 + S_ENDPGM 0 +... + +#--- valid-forever.mir +--- +name: valid_forever +body: | + bb.0: + S_MONITOR_SLEEP_gfx12 32768 + S_MONITOR_SLEEP_gfx12 32895 + S_MONITOR_SLEEP_gfx12 -32768 + S_MONITOR_SLEEP_gfx12 -32641 + S_ENDPGM 0 +... + +#--- gfx13.mir +--- +name: valid_duration_and_forever +body: | + bb.0: + S_MONITOR_SLEEP 0 + S_MONITOR_SLEEP 127 + S_MONITOR_SLEEP 32768 + S_MONITOR_SLEEP -32641 + S_MONITOR_SLEEP_gfx13 0 + S_MONITOR_SLEEP_gfx13 127 + S_MONITOR_SLEEP_gfx13 32768 + S_MONITOR_SLEEP_gfx13 32895 + S_MONITOR_SLEEP_gfx13 -32768 + S_MONITOR_SLEEP_gfx13 -32641 + S_ENDPGM 0 +... + +#--- gfx13-duration.mir +--- +name: invalid_duration +body: | + bb.0: + S_MONITOR_SLEEP 128 + S_MONITOR_SLEEP 65536 + S_MONITOR_SLEEP -32640 + S_MONITOR_SLEEP -1 + S_MONITOR_SLEEP_gfx13 200 + S_MONITOR_SLEEP_gfx13 65536 + S_MONITOR_SLEEP_gfx13 32896 + S_MONITOR_SLEEP_gfx13 -32640 + S_MONITOR_SLEEP_gfx13 -1 + S_ENDPGM 0 +... + +#--- reserved-forever.mir +--- +name: reserved_forever_duration +body: | + bb.0: + S_MONITOR_SLEEP -32640 + S_MONITOR_SLEEP -1 + S_MONITOR_SLEEP_gfx12 -32640 + S_MONITOR_SLEEP_gfx12 -1 + S_ENDPGM 0 +... >From 429a609691eced31fadd58048383fd458f212dba Mon Sep 17 00:00:00 2001 From: Yaxun Liu <[email protected]> Date: Sat, 14 Nov 2026 02:48:14 +0000 Subject: [PATCH 6/6] [AMDGPU] Validate monitor-sleep operands across languages Remove the HIP host bypass from constant duration validation and cover native C and OpenCL range diagnostics. AI assisted. --- clang/lib/Sema/SemaAMDGPU.cpp | 2 -- clang/test/Sema/amdgpu-monitor-sleep-range.c | 21 +++++++++++++++++++ .../SemaCUDA/amdgpu-monitor-sleep-range.cu | 5 ++--- .../MC/AMDGPU/monitor-sleep-duration-err.s | 4 ++-- 4 files changed, 25 insertions(+), 7 deletions(-) create mode 100644 clang/test/Sema/amdgpu-monitor-sleep-range.c diff --git a/clang/lib/Sema/SemaAMDGPU.cpp b/clang/lib/Sema/SemaAMDGPU.cpp index 04c73db6d9152..94b7f7ce1f3a8 100644 --- a/clang/lib/Sema/SemaAMDGPU.cpp +++ b/clang/lib/Sema/SemaAMDGPU.cpp @@ -167,8 +167,6 @@ bool SemaAMDGPU::CheckAMDGCNBuiltinFunctionCall(const TargetInfo &TI, return false; } case AMDGPU::BI__builtin_amdgcn_s_monitor_sleep: { - if (getLangOpts().CUDA && !getLangOpts().CUDAIsDevice) - return false; bool NoSleepForever = Builtin::evaluateRequiredTargetFeatures( "no-sleep-forever", CallerFeatureMap); Expr *ArgExpr = TheCall->getArg(0); diff --git a/clang/test/Sema/amdgpu-monitor-sleep-range.c b/clang/test/Sema/amdgpu-monitor-sleep-range.c new file mode 100644 index 0000000000000..f44ff4e26a164 --- /dev/null +++ b/clang/test/Sema/amdgpu-monitor-sleep-range.c @@ -0,0 +1,21 @@ +// REQUIRES: amdgpu-registered-target +// RUN: %clang_cc1 -triple amdgpu12.50-amd-amdhsa -x c -DNO_SLEEP_FOREVER -fsyntax-only -verify=range,forever %s +// RUN: %clang_cc1 -triple amdgpu12.50-amd-amdhsa -x cl -DNO_SLEEP_FOREVER -fsyntax-only -verify=range,forever %s +// RUN: %clang_cc1 -triple amdgpu13-amd-amdhsa -x c -fsyntax-only -verify=range,negative %s +// RUN: %clang_cc1 -triple amdgpu13-amd-amdhsa -x cl -fsyntax-only -verify=range,negative %s + +void monitor_sleep_range(void) { + __builtin_amdgcn_s_monitor_sleep(0); + __builtin_amdgcn_s_monitor_sleep(127); + __builtin_amdgcn_s_monitor_sleep(128); // range-error {{argument value 128 is outside the valid range [0, 127]}} + __builtin_amdgcn_s_monitor_sleep(200); // range-error {{argument value 200 is outside the valid range [0, 127]}} + __builtin_amdgcn_s_monitor_sleep(32767); // range-error {{argument value 32767 is outside the valid range [0, 127]}} +#ifdef NO_SLEEP_FOREVER + __builtin_amdgcn_s_monitor_sleep(-32768); // forever-error {{argument value -32768 is outside the valid range [0, 127]}} +#else + __builtin_amdgcn_s_monitor_sleep(-32768); + __builtin_amdgcn_s_monitor_sleep(-32641); + __builtin_amdgcn_s_monitor_sleep(-32640); // negative-error {{argument value -32640 is outside the valid range [-32768, -32641]}} + __builtin_amdgcn_s_monitor_sleep(-1); // negative-error {{argument value -1 is outside the valid range [-32768, -32641]}} +#endif +} diff --git a/clang/test/SemaCUDA/amdgpu-monitor-sleep-range.cu b/clang/test/SemaCUDA/amdgpu-monitor-sleep-range.cu index 793f9952ee21e..c1c54159a4960 100644 --- a/clang/test/SemaCUDA/amdgpu-monitor-sleep-range.cu +++ b/clang/test/SemaCUDA/amdgpu-monitor-sleep-range.cu @@ -4,12 +4,11 @@ // RUN: %clang_cc1 -triple amdgpu12.50s-amd-amdhsa -fcuda-is-device -x hip -DNO_SLEEP_FOREVER -fsyntax-only -verify=range,forever %s // RUN: %clang_cc1 -triple amdgpu12.51-amd-amdhsa -fcuda-is-device -x hip -fsyntax-only -verify=range,negative %s // RUN: %clang_cc1 -triple amdgpu12.50-amd-amdhsa -target-feature -no-sleep-forever -fcuda-is-device -x hip -fsyntax-only -verify=range,negative %s -// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -aux-triple amdgpu12.50-amd-amdhsa -x hip -DNO_SLEEP_FOREVER -fsyntax-only -verify=host %s -// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -aux-triple amdgpu13-amd-amdhsa -x hip -DNO_SLEEP_FOREVER -fsyntax-only -verify=host %s +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -aux-triple amdgpu12.50-amd-amdhsa -x hip -DNO_SLEEP_FOREVER -fsyntax-only -verify=range %s +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -aux-triple amdgpu13-amd-amdhsa -x hip -DNO_SLEEP_FOREVER -fsyntax-only -verify=range %s // RUN: %clang_cc1 -triple amdgpu12.5-amd-amdhsa -fcuda-is-device -x hip -fsyntax-only -verify=range,negative %s // RUN: %clang_cc1 -triple amdgpu13-amd-amdhsa -fcuda-is-device -x hip -fsyntax-only -verify=range,negative %s // RUN: %clang_cc1 -triple amdgpu13.10-amd-amdhsa -fcuda-is-device -x hip -fsyntax-only -verify=range,negative %s -// host-no-diagnostics #include "Inputs/cuda.h" diff --git a/llvm/test/MC/AMDGPU/monitor-sleep-duration-err.s b/llvm/test/MC/AMDGPU/monitor-sleep-duration-err.s index 0186022c9f049..0a270668cac24 100644 --- a/llvm/test/MC/AMDGPU/monitor-sleep-duration-err.s +++ b/llvm/test/MC/AMDGPU/monitor-sleep-duration-err.s @@ -10,7 +10,7 @@ s_monitor_sleep -1 // FOREVER: :[[@LINE-1]]:17: error: sleep duration must be in range [0, 127] on this target -// NO-FOREVER: :[[@LINE-2]]:17: error: sleep forever is unsuported on the target +// NO-FOREVER: :[[@LINE-2]]:17: error: sleep forever is unsupported on the target s_monitor_sleep 0x10000 // ERR: :[[@LINE-1]]:17: error: sleep duration must be in range [0, 127] on this target @@ -23,7 +23,7 @@ s_monitor_sleep 0x80 s_monitor_sleep 0x8080 // FOREVER: :[[@LINE-1]]:17: error: sleep duration must be in range [0, 127] on this target -// NO-FOREVER: :[[@LINE-2]]:17: error: sleep forever is unsuported on the target +// NO-FOREVER: :[[@LINE-2]]:17: error: sleep forever is unsupported on the target s_monitor_sleep 0xc8 // ERR: :[[@LINE-1]]:17: error: sleep duration must be in range [0, 127] on this target _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
