https://github.com/jjppp updated https://github.com/llvm/llvm-project/pull/201623
>From 3d8c661aeff295f5602fcff6c06e41a3630a9c02 Mon Sep 17 00:00:00 2001 From: jpwang <[email protected]> Date: Fri, 5 Jun 2026 00:37:01 +0800 Subject: [PATCH] [Clang][CodeGen] Avoid failing downcast when __builtin_prefech's immarg is poison --- clang/test/CodeGen/prefetch-poison-rw.c | 6 +++++ .../SelectionDAG/SelectionDAGBuilder.cpp | 27 +++++++++++++------ 2 files changed, 25 insertions(+), 8 deletions(-) create mode 100644 clang/test/CodeGen/prefetch-poison-rw.c diff --git a/clang/test/CodeGen/prefetch-poison-rw.c b/clang/test/CodeGen/prefetch-poison-rw.c new file mode 100644 index 0000000000000..c13d4bb246d54 --- /dev/null +++ b/clang/test/CodeGen/prefetch-poison-rw.c @@ -0,0 +1,6 @@ +// RUN: %clang_cc1 -triple x86_64-pc-linux -emit-llvm %s -o - | FileCheck %s + +void test_poison_rw(void) { + __builtin_prefetch(0, 2 >> 32); + // CHECK: call void @llvm.prefetch.p0(ptr null, i32 0, i32 3, i32 1) +} diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp index eca5bb1598ae0..92b0fb7baa0cc 100644 --- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp @@ -7767,17 +7767,28 @@ void SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I, return; } case Intrinsic::prefetch: { + auto *DefaultConstantInt = + llvm::ConstantInt::get(llvm::Type::getInt32Ty(*DAG.getContext()), 0); + auto GetConstantIntOrDefault = [&](Value *Val) { + if (auto *Constant = dyn_cast<ConstantInt>(Val)) { + return Constant; + } + // Cases where Val is non-constant should be rejected by Sema. + return DefaultConstantInt; + }; + SDValue Ops[5]; - unsigned rw = cast<ConstantInt>(I.getArgOperand(1))->getZExtValue(); - auto Flags = rw == 0 ? MachineMemOperand::MOLoad :MachineMemOperand::MOStore; + unsigned rw = GetConstantIntOrDefault(I.getArgOperand(1))->getZExtValue(); + auto Flags = + rw == 0 ? MachineMemOperand::MOLoad : MachineMemOperand::MOStore; Ops[0] = DAG.getRoot(); Ops[1] = getValue(I.getArgOperand(0)); - Ops[2] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(1)), sdl, - MVT::i32); - Ops[3] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(2)), sdl, - MVT::i32); - Ops[4] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(3)), sdl, - MVT::i32); + Ops[2] = DAG.getTargetConstant(*GetConstantIntOrDefault(I.getArgOperand(1)), + sdl, MVT::i32); + Ops[3] = DAG.getTargetConstant(*GetConstantIntOrDefault(I.getArgOperand(2)), + sdl, MVT::i32); + Ops[4] = DAG.getTargetConstant(*GetConstantIntOrDefault(I.getArgOperand(3)), + sdl, MVT::i32); SDValue Result = DAG.getMemIntrinsicNode( ISD::PREFETCH, sdl, DAG.getVTList(MVT::Other), Ops, EVT::getIntegerVT(*Context, 8), MachinePointerInfo(I.getArgOperand(0)), _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
