[llvm] [NVPTX] Preserve the load chain when custom-lowering i1 loads (PR #230497)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Oct 10 05:52:25 PDT 2026
https://github.com/actinks updated https://github.com/llvm/llvm-project/pull/230497
>From a938ef3961772e7dbfb3682b0e8eef54f4f64380 Mon Sep 17 00:00:00 2001
From: actink <actink at 163.com>
Date: Fri, 9 Oct 2026 21:27:15 +0800
Subject: [PATCH 1/2] [NVPTX] Preserve the load chain when custom-lowering i1
loads
lowerLOADi1() rewrites an i1 load into a zext load to i16 plus a truncate,
and returns the (value, chain) pair as a MERGE_VALUES node.
LegalizeLoadOps installs that pair with
RChain = Res.getValue(1);
DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), RChain);
so the second value of the MERGE_VALUES becomes the replacement for the
original load's chain result. Returning LD->getChain() therefore rewires
every memory operation that followed the original load to that load's
predecessor, and leaves the new zext load's chain result with no users.
The ordering edge between the new load and those memory operations is
dropped, so nothing in the DAG keeps them in order beyond whatever data
dependency happens to exist between them.
Return newLD.getValue(1) instead, so the edge is preserved.
---
llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp b/llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp
index 2a06c99f66ca9..30b2dddc305fe 100644
--- a/llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp
+++ b/llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp
@@ -4030,7 +4030,7 @@ static SDValue lowerLOADi1(LoadSDNode *LD, SelectionDAG &DAG) {
// The legalizer (the caller) is expecting two values from the legalized
// load, so we build a MergeValues node for it. See ExpandUnalignedLoad()
// in LegalizeDAG.cpp which also uses MergeValues.
- return DAG.getMergeValues({result, LD->getChain()}, dl);
+ return DAG.getMergeValues({result, newLD.getValue(1)}, dl);
}
SDValue NVPTXTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
>From 22ab8652657313b403ff471a8102e25775b3c144 Mon Sep 17 00:00:00 2001
From: actink <actink at 163.com>
Date: Sat, 10 Oct 2026 20:51:54 +0800
Subject: [PATCH 2/2] [NVPTX] Add a test for the i1 load chain
---
llvm/test/CodeGen/NVPTX/i1-load-chain.ll | 20 ++++++++++++++++++++
1 file changed, 20 insertions(+)
create mode 100644 llvm/test/CodeGen/NVPTX/i1-load-chain.ll
diff --git a/llvm/test/CodeGen/NVPTX/i1-load-chain.ll b/llvm/test/CodeGen/NVPTX/i1-load-chain.ll
new file mode 100644
index 0000000000000..13b108c76bdf8
--- /dev/null
+++ b/llvm/test/CodeGen/NVPTX/i1-load-chain.ll
@@ -0,0 +1,20 @@
+; RUN: llc < %s -mtriple=nvptx64 -debug-only=isel 2>&1 | FileCheck %s
+; REQUIRES: asserts
+;
+; An i1 load is custom-lowered to a zext load to i16 plus a truncate.
+; LegalizeLoadOps replaces the original load's chain result with the chain the
+; lowering returns, so the lowering must return the new load's chain: returning
+; the original one leaves the new load unordered with respect to everything
+; that followed the original load. The store to %p aliases the load, and its
+; stored value does not come from the load, so only the chain orders them.
+
+target triple = "nvptx64-nvidia-cuda"
+
+define void @foo(ptr %p, ptr %q) {
+; CHECK: [[LD:t[0-9]+]]: i16,ch = load<{{.*}}load (s8) from %ir.p{{.*}}
+; CHECK: store<(store (s8) into %ir.p), trunc to i8> [[LD]]:1,
+ %v = load i1, ptr %p
+ store i1 true, ptr %p
+ store i1 %v, ptr %q
+ ret void
+}
More information about the llvm-commits
mailing list