[llvm] TableGen: Allow IsTruncStore predicates on atomic PatFrags (PR #229687)
Matt Arsenault via llvm-commits
llvm-commits at lists.llvm.org
Wed Oct 7 01:19:05 PDT 2026
https://github.com/arsenm created https://github.com/llvm/llvm-project/pull/229687
Atomic stores can be truncating in the same way as regular stores.
Allow IsTruncStore and IsNonTruncStore to be set on atomic store
PatFrags so patterns can check the store is not truncating without
needing to specify a fixed memory size.
I still find the hierarchy of load/store PatFrags frustrating. This would be
easier if we fixed the legacy mistake of treating atomic store as an "atomic"
rather than a store.
Co-authored-by: Claude Opus 5.5 <noreply at anthropic.com>
>From df9fdb95314afe71624c21ebcf9991171f7d9593 Mon Sep 17 00:00:00 2001
From: Matt Arsenault <Matthew.Arsenault at amd.com>
Date: Wed, 7 Oct 2026 09:51:35 +0200
Subject: [PATCH] TableGen: Allow IsTruncStore predicates on atomic PatFrags
Atomic stores can be truncating in the same way as regular stores.
Allow IsTruncStore and IsNonTruncStore to be set on atomic store
PatFrags so patterns can check the store is not truncating without
needing to specify a fixed memory size.
I still find the hierarchy of load/store PatFrags frustrating. This would be
easier if we fixed the legacy mistake of treating atomic store as an "atomic"
rather than a store.
Co-authored-by: Claude Opus 5.5 <noreply at anthropic.com>
---
.../TableGen/atomic-store-trunc-patfrags.td | 40 +++++++++++++++++++
.../TableGen/Common/CodeGenDAGPatterns.cpp | 37 +++++++++++++----
llvm/utils/TableGen/GlobalISelEmitter.cpp | 2 +-
3 files changed, 70 insertions(+), 9 deletions(-)
create mode 100644 llvm/test/TableGen/atomic-store-trunc-patfrags.td
diff --git a/llvm/test/TableGen/atomic-store-trunc-patfrags.td b/llvm/test/TableGen/atomic-store-trunc-patfrags.td
new file mode 100644
index 00000000000000..6d6e3848dd249c
--- /dev/null
+++ b/llvm/test/TableGen/atomic-store-trunc-patfrags.td
@@ -0,0 +1,40 @@
+// RUN: llvm-tblgen -gen-dag-isel -I %p/../../include -I %p/Common %s | FileCheck -check-prefix=SDAG %s
+// RUN: llvm-tblgen -gen-global-isel -optimize-match-table=false -I %p/../../include -I %p/Common %s | FileCheck -check-prefix=GISEL %s
+
+include "llvm/Target/Target.td"
+include "GlobalISelEmitterCommon.td"
+
+def atomic_store_nontrunc : PatFrag<(ops node:$val, node:$ptr),
+ (atomic_store node:$val, node:$ptr)> {
+ let IsAtomic = 1;
+ let IsTruncStore = 0;
+}
+
+def atomic_store_trunc : PatFrag<(ops node:$val, node:$ptr),
+ (atomic_store node:$val, node:$ptr)> {
+ let IsAtomic = 1;
+ let IsTruncStore = 1;
+}
+
+def STORE : I<(outs), (ins GPR32:$val, GPR32:$ptr), []>;
+def TRUNCSTORE : I<(outs), (ins GPR32:$val, GPR32:$ptr), []>;
+
+// SDAG-LABEL: CheckNodePredicate(SDValue Op, unsigned PredNo) const
+// SDAG: // Predicate_atomic_store_nontrunc
+// SDAG: if (cast<AtomicSDNode>(N)->getMemoryVT().getSizeInBits() != cast<AtomicSDNode>(N)->getVal().getValueSizeInBits()) return false;
+// SDAG: // Predicate_atomic_store_trunc
+// SDAG: if (cast<AtomicSDNode>(N)->getMemoryVT().getSizeInBits() == cast<AtomicSDNode>(N)->getVal().getValueSizeInBits()) return false;
+
+// GISEL: GIM_CheckOpcode, /*MI*/0, GIMT_Encode2(TargetOpcode::G_STORE),
+// GISEL-NEXT: GIM_CheckAtomicOrderingOrStrongerThan, /*MI*/0, /*Order*/(uint8_t)AtomicOrdering::Unordered,
+// GISEL-NEXT: GIM_CheckMemorySizeEqualToLLT, /*MI*/0, /*MMO*/0, /*OpIdx*/0,
+// GISEL: GIR_MutateOpcode, /*InsnID*/0, /*RecycleInsnID*/0, /*Opcode*/GIMT_Encode2(MyTarget::STORE),
+def : Pat<(atomic_store_nontrunc i32:$val, i32:$ptr),
+ (STORE GPR32:$val, GPR32:$ptr)>;
+
+// GISEL: GIM_CheckOpcode, /*MI*/0, GIMT_Encode2(TargetOpcode::G_STORE),
+// GISEL-NEXT: GIM_CheckAtomicOrderingOrStrongerThan, /*MI*/0, /*Order*/(uint8_t)AtomicOrdering::Unordered,
+// GISEL-NEXT: GIM_CheckMemorySizeLessThanLLT, /*MI*/0, /*MMO*/0, /*OpIdx*/0,
+// GISEL: GIR_MutateOpcode, /*InsnID*/0, /*RecycleInsnID*/0, /*Opcode*/GIMT_Encode2(MyTarget::TRUNCSTORE),
+def : Pat<(atomic_store_trunc i32:$val, i32:$ptr),
+ (TRUNCSTORE GPR32:$val, GPR32:$ptr)>;
diff --git a/llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp b/llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp
index 53eaedd8e3e5ac..d3b7a1af361958 100644
--- a/llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp
+++ b/llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp
@@ -968,22 +968,25 @@ std::string TreePredicateFn::getPredCode() const {
getAddressSpaces() == nullptr && getMinAlignment() < 1)
PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
"IsStore cannot be used by itself");
- } else {
- if (isNonTruncStore())
+ } else if (!isAtomic()) {
+ if (isNonTruncStore()) {
PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
- "IsNonTruncStore requires IsStore");
- if (isTruncStore())
+ "IsNonTruncStore requires IsStore or IsAtomic");
+ }
+
+ if (isTruncStore()) {
PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
- "IsTruncStore requires IsStore");
+ "IsTruncStore requires IsStore or IsAtomic");
+ }
}
if (isAtomic()) {
if (getMemoryVT() == nullptr && getAddressSpaces() == nullptr &&
// FIXME: Should atomic loads be IsLoad, IsAtomic, or both?
!isNonExtLoad() && !isAnyExtLoad() && !isZeroExtLoad() &&
- !isSignExtLoad() && !isAtomicOrderingMonotonic() &&
- !isAtomicOrderingAcquire() && !isAtomicOrderingRelease() &&
- !isAtomicOrderingAcquireRelease() &&
+ !isSignExtLoad() && !isNonTruncStore() && !isTruncStore() &&
+ !isAtomicOrderingMonotonic() && !isAtomicOrderingAcquire() &&
+ !isAtomicOrderingRelease() && !isAtomicOrderingAcquireRelease() &&
!isAtomicOrderingSequentiallyConsistent() &&
!isAtomicOrderingAcquireOrStronger() &&
!isAtomicOrderingReleaseOrStronger() &&
@@ -1107,6 +1110,24 @@ std::string TreePredicateFn::getPredCode() const {
if (isZeroExtLoad())
Code += "if (cast<AtomicSDNode>(N)->getExtensionType() != ISD::ZEXTLOAD) "
"return false;\n";
+
+ if ((isNonTruncStore() + isTruncStore()) > 1) {
+ PrintFatalError(
+ getOrigPatFragRecord()->getRecord()->getLoc(),
+ "IsNonTruncStore, and IsTruncStore are mutually exclusive");
+ }
+
+ if (isNonTruncStore()) {
+ Code += "if (cast<AtomicSDNode>(N)->getMemoryVT().getSizeInBits() != "
+ "cast<AtomicSDNode>(N)->getVal().getValueSizeInBits()) "
+ "return false;\n";
+ }
+
+ if (isTruncStore()) {
+ Code += "if (cast<AtomicSDNode>(N)->getMemoryVT().getSizeInBits() == "
+ "cast<AtomicSDNode>(N)->getVal().getValueSizeInBits()) "
+ "return false;\n";
+ }
}
if (isLoad() || isStore()) {
diff --git a/llvm/utils/TableGen/GlobalISelEmitter.cpp b/llvm/utils/TableGen/GlobalISelEmitter.cpp
index 699229c62df742..be335ce3bc34c9 100644
--- a/llvm/utils/TableGen/GlobalISelEmitter.cpp
+++ b/llvm/utils/TableGen/GlobalISelEmitter.cpp
@@ -663,7 +663,7 @@ Expected<InstructionMatcher &> GlobalISelEmitter::addBuiltinPredicates(
}
}
- if (Predicate.isStore()) {
+ if (Predicate.isStore() || Predicate.isAtomic()) {
if (Predicate.isTruncStore()) {
if (Predicate.getMemoryVT() != nullptr) {
// FIXME: If MemoryVT is set, we end up with 2 checks for the MMO size.
More information about the llvm-commits
mailing list