[llvm] Add 'initialized' attribute langref (PR #84803)
Haopeng Liu via llvm-commits
llvm-commits at lists.llvm.org
Tue Mar 12 15:55:08 PDT 2024
https://github.com/haopliu updated https://github.com/llvm/llvm-project/pull/84803
>From 2554c821dfa3893213f934253ecbca5d9d43ceab Mon Sep 17 00:00:00 2001
From: Haopeng Liu <haopliu at google.com>
Date: Mon, 11 Mar 2024 17:47:08 +0000
Subject: [PATCH 1/3] Add 'initialized' attribute langref
---
llvm/docs/LangRef.rst | 22 ++++++++++++++++++++++
1 file changed, 22 insertions(+)
diff --git a/llvm/docs/LangRef.rst b/llvm/docs/LangRef.rst
index b70220dec92615..39a555edfa80d6 100644
--- a/llvm/docs/LangRef.rst
+++ b/llvm/docs/LangRef.rst
@@ -1621,6 +1621,28 @@ Currently, only the following parameter attributes are defined:
``readonly`` or a ``memory`` attribute that does not contain
``argmem: write``.
+``initialized((Lo1,Hi1),...)``
+ This attribute is a list of const ranges in ascending order with no
+ overlapping or continuous. It indicates that the function initializes the
+ memory through the pointer argument, [%p+LoN, %p+HiN): there are no reads,
+ and no special accesses (such as volatile access or untrackable capture)
+ before the initialization in the function. LoN/HiN are 64-bit ints;
+ negative values are allowed in case a pointer to partway through the
+ allocation is passed to.
+
+ This attribute implies that the function initializes and does not read
+ before initialization through this pointer argument, even though it may
+ read the memory before initialization that the pointer points to, such
+ as through other arguments.
+
+ The ``writable`` or ``dereferenceable`` attribute does not imply
+ ``initialized`` attribute, and ``initialized`` does not imply ``writeonly``
+ since cases that read from the pointer after write, can be ``initialized``
+ but not ``writeonly``.
+
+ Note that this attribute does not apply to the unwind edge: the memory may
+ not actually be written to when unwinding happens.
+
``dead_on_unwind``
At a high level, this attribute indicates that the pointer argument is dead
if the call unwinds, in the sense that the caller will not depend on the
>From a9256bf15dd3e9bb836a467979f61569df98cf8e Mon Sep 17 00:00:00 2001
From: Haopeng Liu <haopliu at google.com>
Date: Tue, 12 Mar 2024 22:10:14 +0000
Subject: [PATCH 2/3] Update the LangRef
---
llvm/docs/LangRef.rst | 27 +++++++++++++++------------
1 file changed, 15 insertions(+), 12 deletions(-)
diff --git a/llvm/docs/LangRef.rst b/llvm/docs/LangRef.rst
index 39a555edfa80d6..0d0f21b8d203ef 100644
--- a/llvm/docs/LangRef.rst
+++ b/llvm/docs/LangRef.rst
@@ -1622,26 +1622,29 @@ Currently, only the following parameter attributes are defined:
``argmem: write``.
``initialized((Lo1,Hi1),...)``
- This attribute is a list of const ranges in ascending order with no
- overlapping or continuous. It indicates that the function initializes the
- memory through the pointer argument, [%p+LoN, %p+HiN): there are no reads,
- and no special accesses (such as volatile access or untrackable capture)
- before the initialization in the function. LoN/HiN are 64-bit ints;
- negative values are allowed in case a pointer to partway through the
- allocation is passed to.
+ This attribute indicates that the function initializes the ranges of the
+ pointer parameter's memory, [%p+LoN, %p+HiN): there are no reads, and no
+ special accesses (such as volatile access or untrackable capture) before
+ the initialization write in the function.
This attribute implies that the function initializes and does not read
before initialization through this pointer argument, even though it may
read the memory before initialization that the pointer points to, such
as through other arguments.
- The ``writable`` or ``dereferenceable`` attribute does not imply
+ Note that this attribute does not apply to the unwind edge: the
+ initializing function does not read the pointer before writing to it
+ regardless of unwinding or not, but the memory may not actually be
+ written to when unwinding happens.
+
+ The ``writable`` or ``dereferenceable`` attribute does not imply the
``initialized`` attribute, and ``initialized`` does not imply ``writeonly``
- since cases that read from the pointer after write, can be ``initialized``
- but not ``writeonly``.
+ since ``initialized`` allows reading from the pointer after writing.
- Note that this attribute does not apply to the unwind edge: the memory may
- not actually be written to when unwinding happens.
+ This attribute is a list of const ranges in ascending order with no
+ overlapping or adjoining list elements. LoN/HiN are 64-bit ints, and
+ negative values are allowed in case the argument points partway into
+ an allocation.
``dead_on_unwind``
At a high level, this attribute indicates that the pointer argument is dead
>From 9150ece33c083b49efed2fca268b4539bf8996b6 Mon Sep 17 00:00:00 2001
From: Haopeng Liu <haopliu at google.com>
Date: Tue, 12 Mar 2024 22:54:21 +0000
Subject: [PATCH 3/3] Refactor the argument uses collecting to a function
---
llvm/lib/Transforms/IPO/FunctionAttrs.cpp | 146 ++++++++++++----------
1 file changed, 83 insertions(+), 63 deletions(-)
diff --git a/llvm/lib/Transforms/IPO/FunctionAttrs.cpp b/llvm/lib/Transforms/IPO/FunctionAttrs.cpp
index 7ebf265e17ba1f..a668dde567c016 100644
--- a/llvm/lib/Transforms/IPO/FunctionAttrs.cpp
+++ b/llvm/lib/Transforms/IPO/FunctionAttrs.cpp
@@ -580,45 +580,14 @@ struct ArgumentUsesTracker : public CaptureTracker {
const SCCNodeSet &SCCNodes;
};
-} // end anonymous namespace
-
-namespace llvm {
-
-template <> struct GraphTraits<ArgumentGraphNode *> {
- using NodeRef = ArgumentGraphNode *;
- using ChildIteratorType = SmallVectorImpl<ArgumentGraphNode *>::iterator;
-
- static NodeRef getEntryNode(NodeRef A) { return A; }
- static ChildIteratorType child_begin(NodeRef N) { return N->Uses.begin(); }
- static ChildIteratorType child_end(NodeRef N) { return N->Uses.end(); }
-};
-
-template <>
-struct GraphTraits<ArgumentGraph *> : public GraphTraits<ArgumentGraphNode *> {
- static NodeRef getEntryNode(ArgumentGraph *AG) { return AG->getEntryNode(); }
-
- static ChildIteratorType nodes_begin(ArgumentGraph *AG) {
- return AG->begin();
- }
-
- static ChildIteratorType nodes_end(ArgumentGraph *AG) { return AG->end(); }
-};
-
-} // end namespace llvm
-
-/// Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.
-static Attribute::AttrKind
-determinePointerAccessAttrs(Argument *A,
- const SmallPtrSet<Argument *, 8> &SCCNodes) {
+/// Get all uses of an argument in the function and store them to different
+/// lists: Reads, Writes, and SpecialUses.
+std::tuple<SmallVector<Instruction *, 16>, SmallVector<Instruction *, 16>,
+ SmallVector<Instruction *, 16>>
+getArgumentUses(Argument *A, const SmallPtrSet<Argument *, 8> &SCCNodes) {
SmallVector<Use *, 32> Worklist;
SmallPtrSet<Use *, 32> Visited;
-
- // inalloca arguments are always clobbered by the call.
- if (A->hasInAllocaAttr() || A->hasPreallocatedAttr())
- return Attribute::None;
-
- bool IsRead = false;
- bool IsWrite = false;
+ SmallVector<Instruction *, 16> Reads, Writes, SpecialUses;
for (Use &U : A->uses()) {
Visited.insert(&U);
@@ -626,10 +595,6 @@ determinePointerAccessAttrs(Argument *A,
}
while (!Worklist.empty()) {
- if (IsWrite && IsRead)
- // No point in searching further..
- return Attribute::None;
-
Use *U = Worklist.pop_back_val();
Instruction *I = cast<Instruction>(U->getUser());
@@ -649,7 +614,7 @@ determinePointerAccessAttrs(Argument *A,
case Instruction::Invoke: {
CallBase &CB = cast<CallBase>(*I);
if (CB.isCallee(U)) {
- IsRead = true;
+ Reads.push_back(I);
// Note that indirect calls do not capture, see comment in
// CaptureTracking for context
continue;
@@ -668,12 +633,15 @@ determinePointerAccessAttrs(Argument *A,
if (Visited.insert(&UU).second)
Worklist.push_back(&UU);
} else if (!CB.doesNotCapture(UseIndex)) {
- if (!CB.onlyReadsMemory())
+ if (!CB.onlyReadsMemory()) {
// If the callee can save a copy into other memory, then simply
// scanning uses of the call is insufficient. We have no way
// of tracking copies of the pointer through memory to see
- // if a reloaded copy is written to, thus we must give up.
- return Attribute::None;
+ // if a reloaded copy is written to, thus we treat it as special
+ // uses.
+ SpecialUses.push_back(I);
+ continue;
+ }
// Push users for processing once we finish this one
if (!I->getType()->isVoidTy())
for (Use &UU : I->uses())
@@ -698,12 +666,12 @@ determinePointerAccessAttrs(Argument *A,
if (CB.doesNotAccessMemory(UseIndex)) {
/* nop */
} else if (!isModSet(ArgMR) || CB.onlyReadsMemory(UseIndex)) {
- IsRead = true;
+ Reads.push_back(I);
} else if (!isRefSet(ArgMR) ||
CB.dataOperandHasImpliedAttr(UseIndex, Attribute::WriteOnly)) {
- IsWrite = true;
+ Writes.push_back(I);
} else {
- return Attribute::None;
+ SpecialUses.push_back(I);
}
break;
}
@@ -711,23 +679,29 @@ determinePointerAccessAttrs(Argument *A,
case Instruction::Load:
// A volatile load has side effects beyond what readonly can be relied
// upon.
- if (cast<LoadInst>(I)->isVolatile())
- return Attribute::None;
+ if (cast<LoadInst>(I)->isVolatile()) {
+ SpecialUses.push_back(I);
+ continue;
+ }
- IsRead = true;
+ Reads.push_back(I);
break;
case Instruction::Store:
- if (cast<StoreInst>(I)->getValueOperand() == *U)
+ if (cast<StoreInst>(I)->getValueOperand() == *U) {
// untrackable capture
- return Attribute::None;
+ SpecialUses.push_back(I);
+ continue;
+ }
// A volatile store has side effects beyond what writeonly can be relied
// upon.
- if (cast<StoreInst>(I)->isVolatile())
- return Attribute::None;
+ if (cast<StoreInst>(I)->isVolatile()) {
+ SpecialUses.push_back(I);
+ continue;
+ }
- IsWrite = true;
+ Writes.push_back(I);
break;
case Instruction::ICmp:
@@ -735,15 +709,55 @@ determinePointerAccessAttrs(Argument *A,
break;
default:
- return Attribute::None;
+ SpecialUses.push_back(I);
}
}
+ return {Reads, Writes, SpecialUses};
+}
+
+} // end anonymous namespace
+
+namespace llvm {
+
+template <> struct GraphTraits<ArgumentGraphNode *> {
+ using NodeRef = ArgumentGraphNode *;
+ using ChildIteratorType = SmallVectorImpl<ArgumentGraphNode *>::iterator;
+
+ static NodeRef getEntryNode(NodeRef A) { return A; }
+ static ChildIteratorType child_begin(NodeRef N) { return N->Uses.begin(); }
+ static ChildIteratorType child_end(NodeRef N) { return N->Uses.end(); }
+};
+
+template <>
+struct GraphTraits<ArgumentGraph *> : public GraphTraits<ArgumentGraphNode *> {
+ static NodeRef getEntryNode(ArgumentGraph *AG) { return AG->getEntryNode(); }
+
+ static ChildIteratorType nodes_begin(ArgumentGraph *AG) {
+ return AG->begin();
+ }
+
+ static ChildIteratorType nodes_end(ArgumentGraph *AG) { return AG->end(); }
+};
+
+} // end namespace llvm
+
+/// Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.
+static Attribute::AttrKind
+determinePointerAccessAttrs(Argument *A, SmallVector<Instruction *, 16> &Reads,
+ SmallVector<Instruction *, 16> &Writes,
+ SmallVector<Instruction *, 16> &SpecialUses) {
+ // inalloca arguments are always clobbered by the call.
+ if (A->hasInAllocaAttr() || A->hasPreallocatedAttr())
+ return Attribute::None;
+
+ if (!SpecialUses.empty())
+ return Attribute::None;
- if (IsWrite && IsRead)
+ if (!Writes.empty() && !Reads.empty())
return Attribute::None;
- else if (IsRead)
+ else if (!Reads.empty())
return Attribute::ReadOnly;
- else if (IsWrite)
+ else if (!Writes.empty())
return Attribute::WriteOnly;
else
return Attribute::ReadNone;
@@ -931,7 +945,9 @@ static void addArgumentAttrs(const SCCNodeSet &SCCNodes,
// functions in the SCC.
SmallPtrSet<Argument *, 8> Self;
Self.insert(&A);
- Attribute::AttrKind R = determinePointerAccessAttrs(&A, Self);
+ auto [Reads, Writes, SpecialUses] = getArgumentUses(&A, Self);
+ Attribute::AttrKind R =
+ determinePointerAccessAttrs(&A, Reads, Writes, SpecialUses);
if (R != Attribute::None)
if (addAccessAttr(&A, R))
Changed.insert(F);
@@ -963,7 +979,9 @@ static void addArgumentAttrs(const SCCNodeSet &SCCNodes,
// Infer the access attributes given the new nocapture one
SmallPtrSet<Argument *, 8> Self;
Self.insert(&*A);
- Attribute::AttrKind R = determinePointerAccessAttrs(&*A, Self);
+ auto [Reads, Writes, SpecialUses] = getArgumentUses(&*A, Self);
+ Attribute::AttrKind R =
+ determinePointerAccessAttrs(&*A, Reads, Writes, SpecialUses);
if (R != Attribute::None)
addAccessAttr(A, R);
}
@@ -1032,7 +1050,9 @@ static void addArgumentAttrs(const SCCNodeSet &SCCNodes,
Attribute::AttrKind AccessAttr = Attribute::ReadNone;
for (ArgumentGraphNode *N : ArgumentSCC) {
Argument *A = N->Definition;
- Attribute::AttrKind K = determinePointerAccessAttrs(A, ArgumentSCCNodes);
+ auto [Reads, Writes, SpecialUses] = getArgumentUses(A, ArgumentSCCNodes);
+ Attribute::AttrKind K =
+ determinePointerAccessAttrs(A, Reads, Writes, SpecialUses);
AccessAttr = meetAccessAttr(AccessAttr, K);
if (AccessAttr == Attribute::None)
break;
More information about the llvm-commits
mailing list