https://github.com/aobolensk created https://github.com/llvm/llvm-project/pull/244410
Swizzle reads of volatile vectors emitted non-volatile loads, so CIR deleted unused reads such as `p->x;` Swizzle store in contrast already keeps the flag >From 0ada39384f7a5b8d2fa4f9e3eb8a666b0305d036 Mon Sep 17 00:00:00 2001 From: Arseniy Obolenskiy <[email protected]> Date: Thu, 19 Nov 2026 23:03:39 +0100 Subject: [PATCH] [CIR] Preserve volatile on ext vector swizzle loads Swizzle reads of volatile vectors emitted non-volatile loads, so CIR deleted unused reads such as `p->x;` Swizzle store in contrast already keeps the flag --- clang/lib/CIR/CodeGen/CIRGenExpr.cpp | 3 +- clang/test/CIR/CodeGenOpenCL/vector.cl | 41 ++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp index 8a885755143092..14703360c608d2 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp @@ -875,7 +875,8 @@ int64_t CIRGenFunction::getAccessedFieldNo(unsigned int idx, // appropriate shufflevector. RValue CIRGenFunction::emitLoadOfExtVectorElementLValue(LValue lv) { mlir::Location loc = lv.getExtVectorPointer().getLoc(); - mlir::Value vec = builder.createLoad(loc, lv.getExtVectorAddress()); + mlir::Value vec = + builder.createLoad(loc, lv.getExtVectorAddress(), lv.isVolatile()); // HLSL allows treating scalars as one-element vectors. Converting the scalar // IR value to a vector here allows the rest of codegen to behave as normal. diff --git a/clang/test/CIR/CodeGenOpenCL/vector.cl b/clang/test/CIR/CodeGenOpenCL/vector.cl index dfb493906d471f..78d130921550b3 100644 --- a/clang/test/CIR/CodeGenOpenCL/vector.cl +++ b/clang/test/CIR/CodeGenOpenCL/vector.cl @@ -244,3 +244,44 @@ int4 vec_init_narrow_swizzle(int2 b) { // LLVM-LABEL: @vec_init_narrow_swizzle // LLVM: %[[RESULT:.*]] = shufflevector <2 x i32> %{{.*}}, <2 x i32> poison, <4 x i32> <i32 1, i32 0, i32 0, i32 1> // LLVM: ret <4 x i32> %[[RESULT]] + +int vec_volatile_swizzle_scalar(volatile int4 *p) { + return p->x; +} + +// CIR-LABEL: cir.func {{.*}} @vec_volatile_swizzle_scalar +// CIR: %[[V:.*]] = cir.load volatile {{.*}} : !cir.ptr<!cir.vector<4 x !s32i>>, !cir.vector<4 x !s32i> +// CIR: cir.vec.extract %[[V]][%{{.*}} : !s64i] : !cir.vector<4 x !s32i> + +// LLVM-LABEL: @vec_volatile_swizzle_scalar +// LLVM: %[[V:.*]] = load volatile <4 x i32>, ptr %{{.*}}, align 16 +// LLVM: %[[X:.*]] = extractelement <4 x i32> %[[V]], i64 0 +// LLVM: ret i32 %[[X]] + +int2 vec_volatile_swizzle_multi(volatile int4 *p) { + return p->xy; +} + +// CIR-LABEL: cir.func {{.*}} @vec_volatile_swizzle_multi +// CIR: %[[V:.*]] = cir.load volatile {{.*}} : !cir.ptr<!cir.vector<4 x !s32i>>, !cir.vector<4 x !s32i> +// CIR: cir.vec.shuffle(%[[V]], %{{.*}} : !cir.vector<4 x !s32i>) [#cir.int<0> : !s32i, #cir.int<1> : !s32i] : !cir.vector<2 x !s32i> + +// LLVM-LABEL: @vec_volatile_swizzle_multi +// LLVM: %[[V:.*]] = load volatile <4 x i32>, ptr %{{.*}}, align 16 +// LLVM: %[[XY:.*]] = shufflevector <4 x i32> %[[V]], <4 x i32> poison, <2 x i32> <i32 0, i32 1> +// LLVM: ret <2 x i32> %[[XY]] + +void vec_volatile_swizzle_unused(volatile int4 *p) { + p->x; + p->xy; +} + +// CIR-LABEL: cir.func {{.*}} @vec_volatile_swizzle_unused +// CIR: cir.load volatile {{.*}} : !cir.ptr<!cir.vector<4 x !s32i>>, !cir.vector<4 x !s32i> +// CIR: cir.load volatile {{.*}} : !cir.ptr<!cir.vector<4 x !s32i>>, !cir.vector<4 x !s32i> +// CIR: cir.return + +// LLVM-LABEL: @vec_volatile_swizzle_unused +// LLVM: load volatile <4 x i32>, ptr %{{.*}}, align 16 +// LLVM: load volatile <4 x i32>, ptr %{{.*}}, align 16 +// LLVM: ret void _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
