[llvm] 7a2c5c6 - [SandboxIR][NFC] Move User into a separate file (#110157)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 26 14:26:02 PDT 2024
Author: vporpo
Date: 2024-09-26T14:25:59-07:00
New Revision: 7a2c5c69ce01cacb18b7838826e442bb981bf9c8
URL: https://github.com/llvm/llvm-project/commit/7a2c5c69ce01cacb18b7838826e442bb981bf9c8
DIFF: https://github.com/llvm/llvm-project/commit/7a2c5c69ce01cacb18b7838826e442bb981bf9c8.diff
LOG: [SandboxIR][NFC] Move User into a separate file (#110157)
Added:
llvm/include/llvm/SandboxIR/User.h
llvm/lib/SandboxIR/User.cpp
Modified:
llvm/include/llvm/SandboxIR/Context.h
llvm/include/llvm/SandboxIR/SandboxIR.h
llvm/lib/SandboxIR/CMakeLists.txt
llvm/lib/SandboxIR/Context.cpp
llvm/lib/SandboxIR/SandboxIR.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/SandboxIR/Context.h b/llvm/include/llvm/SandboxIR/Context.h
index dfba3085c66ac1..092b791bc2acb9 100644
--- a/llvm/include/llvm/SandboxIR/Context.h
+++ b/llvm/include/llvm/SandboxIR/Context.h
@@ -18,6 +18,7 @@ namespace llvm::sandboxir {
class Module;
class Value;
class Argument;
+class Constant;
class Context {
protected:
@@ -69,9 +70,8 @@ class Context {
return getOrCreateValueInternal(LLVMV, 0);
}
/// Get or create a sandboxir::Constant from an existing LLVM IR \p LLVMC.
- Constant *getOrCreateConstant(llvm::Constant *LLVMC) {
- return cast<Constant>(getOrCreateValueInternal(LLVMC, 0));
- }
+ Constant *getOrCreateConstant(llvm::Constant *LLVMC);
+
// Friends for getOrCreateConstant().
#define DEF_CONST(ID, CLASS) friend class CLASS;
#include "llvm/SandboxIR/SandboxIRValues.def"
@@ -158,9 +158,7 @@ class Context {
friend FCmpInst; // For createFCmpInst()
public:
- Context(LLVMContext &LLVMCtx)
- : LLVMCtx(LLVMCtx), IRTracker(*this),
- LLVMIRBuilder(LLVMCtx, ConstantFolder()) {}
+ Context(LLVMContext &LLVMCtx);
Tracker &getTracker() { return IRTracker; }
/// Convenience function for `getTracker().save()`
diff --git a/llvm/include/llvm/SandboxIR/SandboxIR.h b/llvm/include/llvm/SandboxIR/SandboxIR.h
index b32333263c03b8..3d206bca9eae6c 100644
--- a/llvm/include/llvm/SandboxIR/SandboxIR.h
+++ b/llvm/include/llvm/SandboxIR/SandboxIR.h
@@ -114,6 +114,7 @@
#include "llvm/SandboxIR/Tracker.h"
#include "llvm/SandboxIR/Type.h"
#include "llvm/SandboxIR/Use.h"
+#include "llvm/SandboxIR/User.h"
#include "llvm/SandboxIR/Value.h"
#include "llvm/Support/raw_ostream.h"
#include <iterator>
@@ -188,42 +189,6 @@ class CmpInst;
class ICmpInst;
class FCmpInst;
-/// Iterator for the `Use` edges of a User's operands.
-/// \Returns the operand `Use` when dereferenced.
-class OperandUseIterator {
- sandboxir::Use Use;
- /// Don't let the user create a non-empty OperandUseIterator.
- OperandUseIterator(const class Use &Use) : Use(Use) {}
- friend class User; // For constructor
-#define DEF_INSTR(ID, OPC, CLASS) friend class CLASS; // For constructor
-#include "llvm/SandboxIR/SandboxIRValues.def"
-
-public:
- using
diff erence_type = std::ptr
diff _t;
- using value_type = sandboxir::Use;
- using pointer = value_type *;
- using reference = value_type &;
- using iterator_category = std::input_iterator_tag;
-
- OperandUseIterator() = default;
- value_type operator*() const;
- OperandUseIterator &operator++();
- OperandUseIterator operator++(int) {
- auto Copy = *this;
- this->operator++();
- return Copy;
- }
- bool operator==(const OperandUseIterator &Other) const {
- return Use == Other.Use;
- }
- bool operator!=(const OperandUseIterator &Other) const {
- return !(*this == Other);
- }
- OperandUseIterator operator+(unsigned Num) const;
- OperandUseIterator operator-(unsigned Num) const;
- int operator-(const OperandUseIterator &Other) const;
-};
-
/// Argument of a sandboxir::Function.
class Argument : public sandboxir::Value {
Argument(llvm::Argument *Arg, sandboxir::Context &Ctx)
@@ -243,97 +208,6 @@ class Argument : public sandboxir::Value {
#endif
};
-/// A sandboxir::User has operands.
-class User : public Value {
-protected:
- User(ClassID ID, llvm::Value *V, Context &Ctx) : Value(ID, V, Ctx) {}
-
- /// \Returns the Use edge that corresponds to \p OpIdx.
- /// Note: This is the default implementation that works for instructions that
- /// match the underlying LLVM instruction. All others should use a
diff erent
- /// implementation.
- Use getOperandUseDefault(unsigned OpIdx, bool Verify) const;
- /// \Returns the Use for the \p OpIdx'th operand. This is virtual to allow
- /// instructions to deviate from the LLVM IR operands, which is a requirement
- /// for sandboxir Instructions that consist of more than one LLVM Instruction.
- virtual Use getOperandUseInternal(unsigned OpIdx, bool Verify) const = 0;
- friend class OperandUseIterator; // for getOperandUseInternal()
-
- /// The default implementation works only for single-LLVMIR-instruction
- /// Users and only if they match exactly the LLVM instruction.
- unsigned getUseOperandNoDefault(const Use &Use) const {
- return Use.LLVMUse->getOperandNo();
- }
- /// \Returns the operand index of \p Use.
- virtual unsigned getUseOperandNo(const Use &Use) const = 0;
- friend unsigned Use::getOperandNo() const; // For getUseOperandNo()
-
- void swapOperandsInternal(unsigned OpIdxA, unsigned OpIdxB) {
- assert(OpIdxA < getNumOperands() && "OpIdxA out of bounds!");
- assert(OpIdxB < getNumOperands() && "OpIdxB out of bounds!");
- auto UseA = getOperandUse(OpIdxA);
- auto UseB = getOperandUse(OpIdxB);
- UseA.swap(UseB);
- }
-
-#ifndef NDEBUG
- void verifyUserOfLLVMUse(const llvm::Use &Use) const;
-#endif // NDEBUG
-
-public:
- /// For isa/dyn_cast.
- static bool classof(const Value *From);
- using op_iterator = OperandUseIterator;
- using const_op_iterator = OperandUseIterator;
- using op_range = iterator_range<op_iterator>;
- using const_op_range = iterator_range<const_op_iterator>;
-
- virtual op_iterator op_begin() {
- assert(isa<llvm::User>(Val) && "Expect User value!");
- return op_iterator(getOperandUseInternal(0, /*Verify=*/false));
- }
- virtual op_iterator op_end() {
- assert(isa<llvm::User>(Val) && "Expect User value!");
- return op_iterator(
- getOperandUseInternal(getNumOperands(), /*Verify=*/false));
- }
- virtual const_op_iterator op_begin() const {
- return const_cast<User *>(this)->op_begin();
- }
- virtual const_op_iterator op_end() const {
- return const_cast<User *>(this)->op_end();
- }
-
- op_range operands() { return make_range<op_iterator>(op_begin(), op_end()); }
- const_op_range operands() const {
- return make_range<const_op_iterator>(op_begin(), op_end());
- }
- Value *getOperand(unsigned OpIdx) const { return getOperandUse(OpIdx).get(); }
- /// \Returns the operand edge for \p OpIdx. NOTE: This should also work for
- /// OpIdx == getNumOperands(), which is used for op_end().
- Use getOperandUse(unsigned OpIdx) const {
- return getOperandUseInternal(OpIdx, /*Verify=*/true);
- }
- virtual unsigned getNumOperands() const {
- return isa<llvm::User>(Val) ? cast<llvm::User>(Val)->getNumOperands() : 0;
- }
-
- virtual void setOperand(unsigned OperandIdx, Value *Operand);
- /// Replaces any operands that match \p FromV with \p ToV. Returns whether any
- /// operands were replaced.
- bool replaceUsesOfWith(Value *FromV, Value *ToV);
-
-#ifndef NDEBUG
- void verify() const override {
- assert(isa<llvm::User>(Val) && "Expected User!");
- }
- void dumpCommonHeader(raw_ostream &OS) const final;
- void dumpOS(raw_ostream &OS) const override {
- // TODO: Remove this tmp implementation once we get the Instruction classes.
- }
-#endif
-};
-
class Constant : public sandboxir::User {
protected:
Constant(llvm::Constant *C, sandboxir::Context &SBCtx)
diff --git a/llvm/include/llvm/SandboxIR/User.h b/llvm/include/llvm/SandboxIR/User.h
new file mode 100644
index 00000000000000..5e47ba5e727f4f
--- /dev/null
+++ b/llvm/include/llvm/SandboxIR/User.h
@@ -0,0 +1,150 @@
+//===- User.h ---------------------------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_SANDBOXIR_USER_H
+#define LLVM_SANDBOXIR_USER_H
+
+#include "llvm/IR/User.h"
+#include "llvm/IR/Value.h"
+#include "llvm/SandboxIR/Use.h"
+#include "llvm/SandboxIR/Value.h"
+
+namespace llvm::sandboxir {
+
+class Context;
+
+/// Iterator for the `Use` edges of a User's operands.
+/// \Returns the operand `Use` when dereferenced.
+class OperandUseIterator {
+ sandboxir::Use Use;
+ /// Don't let the user create a non-empty OperandUseIterator.
+ OperandUseIterator(const class Use &Use) : Use(Use) {}
+ friend class User; // For constructor
+#define DEF_INSTR(ID, OPC, CLASS) friend class CLASS; // For constructor
+#include "llvm/SandboxIR/SandboxIRValues.def"
+
+public:
+ using
diff erence_type = std::ptr
diff _t;
+ using value_type = sandboxir::Use;
+ using pointer = value_type *;
+ using reference = value_type &;
+ using iterator_category = std::input_iterator_tag;
+
+ OperandUseIterator() = default;
+ value_type operator*() const;
+ OperandUseIterator &operator++();
+ OperandUseIterator operator++(int) {
+ auto Copy = *this;
+ this->operator++();
+ return Copy;
+ }
+ bool operator==(const OperandUseIterator &Other) const {
+ return Use == Other.Use;
+ }
+ bool operator!=(const OperandUseIterator &Other) const {
+ return !(*this == Other);
+ }
+ OperandUseIterator operator+(unsigned Num) const;
+ OperandUseIterator operator-(unsigned Num) const;
+ int operator-(const OperandUseIterator &Other) const;
+};
+
+/// A sandboxir::User has operands.
+class User : public Value {
+protected:
+ User(ClassID ID, llvm::Value *V, Context &Ctx) : Value(ID, V, Ctx) {}
+
+ /// \Returns the Use edge that corresponds to \p OpIdx.
+ /// Note: This is the default implementation that works for instructions that
+ /// match the underlying LLVM instruction. All others should use a
diff erent
+ /// implementation.
+ Use getOperandUseDefault(unsigned OpIdx, bool Verify) const;
+ /// \Returns the Use for the \p OpIdx'th operand. This is virtual to allow
+ /// instructions to deviate from the LLVM IR operands, which is a requirement
+ /// for sandboxir Instructions that consist of more than one LLVM Instruction.
+ virtual Use getOperandUseInternal(unsigned OpIdx, bool Verify) const = 0;
+ friend class OperandUseIterator; // for getOperandUseInternal()
+
+ /// The default implementation works only for single-LLVMIR-instruction
+ /// Users and only if they match exactly the LLVM instruction.
+ unsigned getUseOperandNoDefault(const Use &Use) const {
+ return Use.LLVMUse->getOperandNo();
+ }
+ /// \Returns the operand index of \p Use.
+ virtual unsigned getUseOperandNo(const Use &Use) const = 0;
+ friend unsigned Use::getOperandNo() const; // For getUseOperandNo()
+
+ void swapOperandsInternal(unsigned OpIdxA, unsigned OpIdxB) {
+ assert(OpIdxA < getNumOperands() && "OpIdxA out of bounds!");
+ assert(OpIdxB < getNumOperands() && "OpIdxB out of bounds!");
+ auto UseA = getOperandUse(OpIdxA);
+ auto UseB = getOperandUse(OpIdxB);
+ UseA.swap(UseB);
+ }
+
+#ifndef NDEBUG
+ void verifyUserOfLLVMUse(const llvm::Use &Use) const;
+#endif // NDEBUG
+
+public:
+ /// For isa/dyn_cast.
+ static bool classof(const Value *From);
+ using op_iterator = OperandUseIterator;
+ using const_op_iterator = OperandUseIterator;
+ using op_range = iterator_range<op_iterator>;
+ using const_op_range = iterator_range<const_op_iterator>;
+
+ virtual op_iterator op_begin() {
+ assert(isa<llvm::User>(Val) && "Expect User value!");
+ return op_iterator(getOperandUseInternal(0, /*Verify=*/false));
+ }
+ virtual op_iterator op_end() {
+ assert(isa<llvm::User>(Val) && "Expect User value!");
+ return op_iterator(
+ getOperandUseInternal(getNumOperands(), /*Verify=*/false));
+ }
+ virtual const_op_iterator op_begin() const {
+ return const_cast<User *>(this)->op_begin();
+ }
+ virtual const_op_iterator op_end() const {
+ return const_cast<User *>(this)->op_end();
+ }
+
+ op_range operands() { return make_range<op_iterator>(op_begin(), op_end()); }
+ const_op_range operands() const {
+ return make_range<const_op_iterator>(op_begin(), op_end());
+ }
+ Value *getOperand(unsigned OpIdx) const { return getOperandUse(OpIdx).get(); }
+ /// \Returns the operand edge for \p OpIdx. NOTE: This should also work for
+ /// OpIdx == getNumOperands(), which is used for op_end().
+ Use getOperandUse(unsigned OpIdx) const {
+ return getOperandUseInternal(OpIdx, /*Verify=*/true);
+ }
+ virtual unsigned getNumOperands() const {
+ return isa<llvm::User>(Val) ? cast<llvm::User>(Val)->getNumOperands() : 0;
+ }
+
+ virtual void setOperand(unsigned OperandIdx, Value *Operand);
+ /// Replaces any operands that match \p FromV with \p ToV. Returns whether any
+ /// operands were replaced.
+ bool replaceUsesOfWith(Value *FromV, Value *ToV);
+
+#ifndef NDEBUG
+ void verify() const override {
+ assert(isa<llvm::User>(Val) && "Expected User!");
+ }
+ void dumpCommonHeader(raw_ostream &OS) const final;
+ void dumpOS(raw_ostream &OS) const override {
+ // TODO: Remove this tmp implementation once we get the Instruction classes.
+ }
+#endif
+};
+
+} // namespace llvm::sandboxir
+
+#endif // LLVM_SANDBOXIR_USER_H
diff --git a/llvm/lib/SandboxIR/CMakeLists.txt b/llvm/lib/SandboxIR/CMakeLists.txt
index bd91e8dff8a8e5..6386fc908388a0 100644
--- a/llvm/lib/SandboxIR/CMakeLists.txt
+++ b/llvm/lib/SandboxIR/CMakeLists.txt
@@ -6,6 +6,7 @@ add_llvm_component_library(LLVMSandboxIR
SandboxIR.cpp
Tracker.cpp
Type.cpp
+ User.cpp
Value.cpp
ADDITIONAL_HEADER_DIRS
diff --git a/llvm/lib/SandboxIR/Context.cpp b/llvm/lib/SandboxIR/Context.cpp
index 1dc239ba482880..d10cb18e6d3684 100644
--- a/llvm/lib/SandboxIR/Context.cpp
+++ b/llvm/lib/SandboxIR/Context.cpp
@@ -409,6 +409,10 @@ Argument *Context::getOrCreateArgument(llvm::Argument *LLVMArg) {
return cast<Argument>(It->second.get());
}
+Constant *Context::getOrCreateConstant(llvm::Constant *LLVMC) {
+ return cast<Constant>(getOrCreateValueInternal(LLVMC, 0));
+}
+
BasicBlock *Context::createBasicBlock(llvm::BasicBlock *LLVMBB) {
assert(getValue(LLVMBB) == nullptr && "Already exists!");
auto NewBBPtr = std::unique_ptr<BasicBlock>(new BasicBlock(LLVMBB, *this));
@@ -662,6 +666,10 @@ Value *Context::getValue(llvm::Value *V) const {
return nullptr;
}
+Context::Context(LLVMContext &LLVMCtx)
+ : LLVMCtx(LLVMCtx), IRTracker(*this),
+ LLVMIRBuilder(LLVMCtx, ConstantFolder()) {}
+
Module *Context::getModule(llvm::Module *LLVMM) const {
auto It = LLVMModuleToModuleMap.find(LLVMM);
if (It != LLVMModuleToModuleMap.end())
diff --git a/llvm/lib/SandboxIR/SandboxIR.cpp b/llvm/lib/SandboxIR/SandboxIR.cpp
index 17c77f470549e0..92b1ebeedc55b0 100644
--- a/llvm/lib/SandboxIR/SandboxIR.cpp
+++ b/llvm/lib/SandboxIR/SandboxIR.cpp
@@ -115,66 +115,6 @@ void Argument::dumpOS(raw_ostream &OS) const {
}
#endif // NDEBUG
-Use User::getOperandUseDefault(unsigned OpIdx, bool Verify) const {
- assert((!Verify || OpIdx < getNumOperands()) && "Out of bounds!");
- assert(isa<llvm::User>(Val) && "Non-users have no operands!");
- llvm::Use *LLVMUse;
- if (OpIdx != getNumOperands())
- LLVMUse = &cast<llvm::User>(Val)->getOperandUse(OpIdx);
- else
- LLVMUse = cast<llvm::User>(Val)->op_end();
- return Use(LLVMUse, const_cast<User *>(this), Ctx);
-}
-
-#ifndef NDEBUG
-void User::verifyUserOfLLVMUse(const llvm::Use &Use) const {
- assert(Ctx.getValue(Use.getUser()) == this &&
- "Use not found in this SBUser's operands!");
-}
-#endif
-
-bool User::classof(const Value *From) {
- switch (From->getSubclassID()) {
-#define DEF_VALUE(ID, CLASS)
-#define DEF_USER(ID, CLASS) \
- case ClassID::ID: \
- return true;
-#define DEF_INSTR(ID, OPC, CLASS) \
- case ClassID::ID: \
- return true;
-#include "llvm/SandboxIR/SandboxIRValues.def"
- default:
- return false;
- }
-}
-
-void User::setOperand(unsigned OperandIdx, Value *Operand) {
- assert(isa<llvm::User>(Val) && "No operands!");
- Ctx.getTracker().emplaceIfTracking<UseSet>(getOperandUse(OperandIdx));
- // We are delegating to llvm::User::setOperand().
- cast<llvm::User>(Val)->setOperand(OperandIdx, Operand->Val);
-}
-
-bool User::replaceUsesOfWith(Value *FromV, Value *ToV) {
- auto &Tracker = Ctx.getTracker();
- if (Tracker.isTracking()) {
- for (auto OpIdx : seq<unsigned>(0, getNumOperands())) {
- auto Use = getOperandUse(OpIdx);
- if (Use.get() == FromV)
- Tracker.emplaceIfTracking<UseSet>(Use);
- }
- }
- // We are delegating RUOW to LLVM IR's RUOW.
- return cast<llvm::User>(Val)->replaceUsesOfWith(FromV->Val, ToV->Val);
-}
-
-#ifndef NDEBUG
-void User::dumpCommonHeader(raw_ostream &OS) const {
- Value::dumpCommonHeader(OS);
- // TODO: This is incomplete
-}
-#endif // NDEBUG
-
BBIterator &BBIterator::operator++() {
auto ItE = BB->end();
assert(It != ItE && "Already at end!");
diff --git a/llvm/lib/SandboxIR/User.cpp b/llvm/lib/SandboxIR/User.cpp
new file mode 100644
index 00000000000000..8afa52e32b7622
--- /dev/null
+++ b/llvm/lib/SandboxIR/User.cpp
@@ -0,0 +1,74 @@
+//===- User.cpp - The User class of Sandbox IR ----------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/SandboxIR/User.h"
+#include "llvm/SandboxIR/Context.h"
+
+namespace llvm::sandboxir {
+
+Use User::getOperandUseDefault(unsigned OpIdx, bool Verify) const {
+ assert((!Verify || OpIdx < getNumOperands()) && "Out of bounds!");
+ assert(isa<llvm::User>(Val) && "Non-users have no operands!");
+ llvm::Use *LLVMUse;
+ if (OpIdx != getNumOperands())
+ LLVMUse = &cast<llvm::User>(Val)->getOperandUse(OpIdx);
+ else
+ LLVMUse = cast<llvm::User>(Val)->op_end();
+ return Use(LLVMUse, const_cast<User *>(this), Ctx);
+}
+
+#ifndef NDEBUG
+void User::verifyUserOfLLVMUse(const llvm::Use &Use) const {
+ assert(Ctx.getValue(Use.getUser()) == this &&
+ "Use not found in this SBUser's operands!");
+}
+#endif
+
+bool User::classof(const Value *From) {
+ switch (From->getSubclassID()) {
+#define DEF_VALUE(ID, CLASS)
+#define DEF_USER(ID, CLASS) \
+ case ClassID::ID: \
+ return true;
+#define DEF_INSTR(ID, OPC, CLASS) \
+ case ClassID::ID: \
+ return true;
+#include "llvm/SandboxIR/SandboxIRValues.def"
+ default:
+ return false;
+ }
+}
+
+void User::setOperand(unsigned OperandIdx, Value *Operand) {
+ assert(isa<llvm::User>(Val) && "No operands!");
+ Ctx.getTracker().emplaceIfTracking<UseSet>(getOperandUse(OperandIdx));
+ // We are delegating to llvm::User::setOperand().
+ cast<llvm::User>(Val)->setOperand(OperandIdx, Operand->Val);
+}
+
+bool User::replaceUsesOfWith(Value *FromV, Value *ToV) {
+ auto &Tracker = Ctx.getTracker();
+ if (Tracker.isTracking()) {
+ for (auto OpIdx : seq<unsigned>(0, getNumOperands())) {
+ auto Use = getOperandUse(OpIdx);
+ if (Use.get() == FromV)
+ Tracker.emplaceIfTracking<UseSet>(Use);
+ }
+ }
+ // We are delegating RUOW to LLVM IR's RUOW.
+ return cast<llvm::User>(Val)->replaceUsesOfWith(FromV->Val, ToV->Val);
+}
+
+#ifndef NDEBUG
+void User::dumpCommonHeader(raw_ostream &OS) const {
+ Value::dumpCommonHeader(OS);
+ // TODO: This is incomplete
+}
+#endif // NDEBUG
+
+} // namespace llvm::sandboxir
More information about the llvm-commits
mailing list