From d66532d6d44eb8d799cd831d28a7d42aaf025191 Mon Sep 17 00:00:00 2001 From: Vasileios Porpodas Date: Mon, 30 Sep 2024 08:46:20 -0700 Subject: [PATCH] [SandboxIR][NFC] Move Function class to a separate file --- llvm/benchmarks/SandboxIRBench.cpp | 1 + llvm/include/llvm/SandboxIR/Constant.h | 53 -------------- llvm/include/llvm/SandboxIR/Function.h | 72 +++++++++++++++++++ llvm/lib/SandboxIR/BasicBlock.cpp | 1 + llvm/lib/SandboxIR/CMakeLists.txt | 1 + llvm/lib/SandboxIR/Constant.cpp | 41 +---------- llvm/lib/SandboxIR/Context.cpp | 1 + llvm/lib/SandboxIR/Function.cpp | 55 ++++++++++++++ llvm/lib/SandboxIR/Instruction.cpp | 1 + llvm/lib/SandboxIR/Module.cpp | 1 + llvm/lib/SandboxIR/Region.cpp | 1 + .../SandboxVectorizer/Passes/BottomUpVec.cpp | 1 + llvm/unittests/SandboxIR/PassTest.cpp | 1 + llvm/unittests/SandboxIR/RegionTest.cpp | 2 +- llvm/unittests/SandboxIR/SandboxIRTest.cpp | 1 + llvm/unittests/SandboxIR/TrackerTest.cpp | 1 + llvm/unittests/SandboxIR/TypesTest.cpp | 1 + llvm/unittests/SandboxIR/UtilsTest.cpp | 1 + .../SandboxVectorizer/DependencyGraphTest.cpp | 2 +- .../SandboxVectorizer/IntervalTest.cpp | 2 +- .../SandboxVectorizer/LegalityTest.cpp | 1 + 21 files changed, 145 insertions(+), 96 deletions(-) create mode 100644 llvm/include/llvm/SandboxIR/Function.h create mode 100644 llvm/lib/SandboxIR/Function.cpp diff --git a/llvm/benchmarks/SandboxIRBench.cpp b/llvm/benchmarks/SandboxIRBench.cpp index 8f7ab1a376899..45f352697868b 100644 --- a/llvm/benchmarks/SandboxIRBench.cpp +++ b/llvm/benchmarks/SandboxIRBench.cpp @@ -18,6 +18,7 @@ #include "llvm/IR/Function.h" #include "llvm/IR/Instruction.h" #include "llvm/IR/Module.h" +#include "llvm/SandboxIR/Function.h" #include "llvm/SandboxIR/Instruction.h" #include "llvm/SandboxIR/Module.h" #include "llvm/Support/SourceMgr.h" diff --git a/llvm/include/llvm/SandboxIR/Constant.h b/llvm/include/llvm/SandboxIR/Constant.h index e35d23be6619f..7965f947e31b8 100644 --- a/llvm/include/llvm/SandboxIR/Constant.h +++ b/llvm/include/llvm/SandboxIR/Constant.h @@ -1227,59 +1227,6 @@ class ConstantTokenNone final : public Constant { #endif }; -class Function : public GlobalWithNodeAPI { - /// Helper for mapped_iterator. - struct LLVMBBToBB { - Context &Ctx; - LLVMBBToBB(Context &Ctx) : Ctx(Ctx) {} - BasicBlock &operator()(llvm::BasicBlock &LLVMBB) const { - return *cast(Ctx.getValue(&LLVMBB)); - } - }; - /// Use Context::createFunction() instead. - Function(llvm::Function *F, sandboxir::Context &Ctx) - : GlobalWithNodeAPI(ClassID::Function, F, Ctx) {} - friend class Context; // For constructor. - -public: - /// For isa/dyn_cast. - static bool classof(const sandboxir::Value *From) { - return From->getSubclassID() == ClassID::Function; - } - - Module *getParent() { - return Ctx.getModule(cast(Val)->getParent()); - } - - Argument *getArg(unsigned Idx) const { - llvm::Argument *Arg = cast(Val)->getArg(Idx); - return cast(Ctx.getValue(Arg)); - } - - size_t arg_size() const { return cast(Val)->arg_size(); } - bool arg_empty() const { return cast(Val)->arg_empty(); } - - using iterator = mapped_iterator; - iterator begin() const { - LLVMBBToBB BBGetter(Ctx); - return iterator(cast(Val)->begin(), BBGetter); - } - iterator end() const { - LLVMBBToBB BBGetter(Ctx); - return iterator(cast(Val)->end(), BBGetter); - } - FunctionType *getFunctionType() const; - -#ifndef NDEBUG - void verify() const final { - assert(isa(Val) && "Expected Function!"); - } - void dumpNameAndArgs(raw_ostream &OS) const; - void dumpOS(raw_ostream &OS) const final; -#endif -}; - } // namespace llvm::sandboxir #endif // LLVM_SANDBOXIR_CONSTANT_H diff --git a/llvm/include/llvm/SandboxIR/Function.h b/llvm/include/llvm/SandboxIR/Function.h new file mode 100644 index 0000000000000..a810533f769f7 --- /dev/null +++ b/llvm/include/llvm/SandboxIR/Function.h @@ -0,0 +1,72 @@ +//===- Function.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_FUNCTION_H +#define LLVM_SANDBOXIR_FUNCTION_H + +#include "llvm/IR/Function.h" +#include "llvm/SandboxIR/Constant.h" + +namespace llvm::sandboxir { + +class Function : public GlobalWithNodeAPI { + /// Helper for mapped_iterator. + struct LLVMBBToBB { + Context &Ctx; + LLVMBBToBB(Context &Ctx) : Ctx(Ctx) {} + BasicBlock &operator()(llvm::BasicBlock &LLVMBB) const { + return *cast(Ctx.getValue(&LLVMBB)); + } + }; + /// Use Context::createFunction() instead. + Function(llvm::Function *F, sandboxir::Context &Ctx) + : GlobalWithNodeAPI(ClassID::Function, F, Ctx) {} + friend class Context; // For constructor. + +public: + /// For isa/dyn_cast. + static bool classof(const sandboxir::Value *From) { + return From->getSubclassID() == ClassID::Function; + } + + Module *getParent() { + return Ctx.getModule(cast(Val)->getParent()); + } + + Argument *getArg(unsigned Idx) const { + llvm::Argument *Arg = cast(Val)->getArg(Idx); + return cast(Ctx.getValue(Arg)); + } + + size_t arg_size() const { return cast(Val)->arg_size(); } + bool arg_empty() const { return cast(Val)->arg_empty(); } + + using iterator = mapped_iterator; + iterator begin() const { + LLVMBBToBB BBGetter(Ctx); + return iterator(cast(Val)->begin(), BBGetter); + } + iterator end() const { + LLVMBBToBB BBGetter(Ctx); + return iterator(cast(Val)->end(), BBGetter); + } + FunctionType *getFunctionType() const; + +#ifndef NDEBUG + void verify() const final { + assert(isa(Val) && "Expected Function!"); + } + void dumpNameAndArgs(raw_ostream &OS) const; + void dumpOS(raw_ostream &OS) const final; +#endif +}; + +} // namespace llvm::sandboxir + +#endif // LLVM_SANDBOXIR_FUNCTION_H diff --git a/llvm/lib/SandboxIR/BasicBlock.cpp b/llvm/lib/SandboxIR/BasicBlock.cpp index ebca41aa39da8..983a5e8b8825e 100644 --- a/llvm/lib/SandboxIR/BasicBlock.cpp +++ b/llvm/lib/SandboxIR/BasicBlock.cpp @@ -8,6 +8,7 @@ #include "llvm/SandboxIR/BasicBlock.h" #include "llvm/SandboxIR/Context.h" +#include "llvm/SandboxIR/Function.h" #include "llvm/SandboxIR/Instruction.h" namespace llvm::sandboxir { diff --git a/llvm/lib/SandboxIR/CMakeLists.txt b/llvm/lib/SandboxIR/CMakeLists.txt index 293be1849f29d..3ec53b04b046f 100644 --- a/llvm/lib/SandboxIR/CMakeLists.txt +++ b/llvm/lib/SandboxIR/CMakeLists.txt @@ -3,6 +3,7 @@ add_llvm_component_library(LLVMSandboxIR BasicBlock.cpp Constant.cpp Context.cpp + Function.cpp Instruction.cpp Module.cpp Pass.cpp diff --git a/llvm/lib/SandboxIR/Constant.cpp b/llvm/lib/SandboxIR/Constant.cpp index 6f1eb1e74347d..f26c004f63092 100644 --- a/llvm/lib/SandboxIR/Constant.cpp +++ b/llvm/lib/SandboxIR/Constant.cpp @@ -10,6 +10,7 @@ #include "llvm/SandboxIR/Argument.h" #include "llvm/SandboxIR/BasicBlock.h" #include "llvm/SandboxIR/Context.h" +#include "llvm/SandboxIR/Function.h" namespace llvm::sandboxir { @@ -467,44 +468,4 @@ GlobalValue *DSOLocalEquivalent::getGlobalValue() const { Ctx.getValue(cast(Val)->getGlobalValue())); } -FunctionType *Function::getFunctionType() const { - return cast( - Ctx.getType(cast(Val)->getFunctionType())); -} - -#ifndef NDEBUG -void Function::dumpNameAndArgs(raw_ostream &OS) const { - auto *F = cast(Val); - OS << *F->getReturnType() << " @" << F->getName() << "("; - interleave( - F->args(), - [this, &OS](const llvm::Argument &LLVMArg) { - auto *SBArg = cast_or_null(Ctx.getValue(&LLVMArg)); - if (SBArg == nullptr) - OS << "NULL"; - else - SBArg->printAsOperand(OS); - }, - [&] { OS << ", "; }); - OS << ")"; -} - -void Function::dumpOS(raw_ostream &OS) const { - dumpNameAndArgs(OS); - OS << " {\n"; - auto *LLVMF = cast(Val); - interleave( - *LLVMF, - [this, &OS](const llvm::BasicBlock &LLVMBB) { - auto *BB = cast_or_null(Ctx.getValue(&LLVMBB)); - if (BB == nullptr) - OS << "NULL"; - else - OS << *BB; - }, - [&OS] { OS << "\n"; }); - OS << "}\n"; -} -#endif // NDEBUG - } // namespace llvm::sandboxir diff --git a/llvm/lib/SandboxIR/Context.cpp b/llvm/lib/SandboxIR/Context.cpp index 0a61e329b78c5..f5b3d2733344f 100644 --- a/llvm/lib/SandboxIR/Context.cpp +++ b/llvm/lib/SandboxIR/Context.cpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #include "llvm/SandboxIR/Context.h" +#include "llvm/SandboxIR/Function.h" #include "llvm/SandboxIR/Instruction.h" #include "llvm/SandboxIR/Module.h" diff --git a/llvm/lib/SandboxIR/Function.cpp b/llvm/lib/SandboxIR/Function.cpp new file mode 100644 index 0000000000000..f7a1d35b00465 --- /dev/null +++ b/llvm/lib/SandboxIR/Function.cpp @@ -0,0 +1,55 @@ +//===- Function.cpp - The Function 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/Function.h" +#include "llvm/IR/Value.h" +#include "llvm/SandboxIR/Context.h" + +namespace llvm::sandboxir { + +FunctionType *Function::getFunctionType() const { + return cast( + Ctx.getType(cast(Val)->getFunctionType())); +} + +#ifndef NDEBUG +void Function::dumpNameAndArgs(raw_ostream &OS) const { + auto *F = cast(Val); + OS << *F->getReturnType() << " @" << F->getName() << "("; + interleave( + F->args(), + [this, &OS](const llvm::Argument &LLVMArg) { + auto *SBArg = cast_or_null(Ctx.getValue(&LLVMArg)); + if (SBArg == nullptr) + OS << "NULL"; + else + SBArg->printAsOperand(OS); + }, + [&] { OS << ", "; }); + OS << ")"; +} + +void Function::dumpOS(raw_ostream &OS) const { + dumpNameAndArgs(OS); + OS << " {\n"; + auto *LLVMF = cast(Val); + interleave( + *LLVMF, + [this, &OS](const llvm::BasicBlock &LLVMBB) { + auto *BB = cast_or_null(Ctx.getValue(&LLVMBB)); + if (BB == nullptr) + OS << "NULL"; + else + OS << *BB; + }, + [&OS] { OS << "\n"; }); + OS << "}\n"; +} +#endif // NDEBUG + +} // namespace llvm::sandboxir diff --git a/llvm/lib/SandboxIR/Instruction.cpp b/llvm/lib/SandboxIR/Instruction.cpp index 919a44fca8b04..b492af893794f 100644 --- a/llvm/lib/SandboxIR/Instruction.cpp +++ b/llvm/lib/SandboxIR/Instruction.cpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #include "llvm/SandboxIR/Instruction.h" +#include "llvm/SandboxIR/Function.h" namespace llvm::sandboxir { diff --git a/llvm/lib/SandboxIR/Module.cpp b/llvm/lib/SandboxIR/Module.cpp index a6a5fb2aae8a1..61cc2414c45ae 100644 --- a/llvm/lib/SandboxIR/Module.cpp +++ b/llvm/lib/SandboxIR/Module.cpp @@ -9,6 +9,7 @@ #include "llvm/SandboxIR/Module.h" #include "llvm/SandboxIR/Constant.h" #include "llvm/SandboxIR/Context.h" +#include "llvm/SandboxIR/Function.h" #include "llvm/SandboxIR/Value.h" using namespace llvm::sandboxir; diff --git a/llvm/lib/SandboxIR/Region.cpp b/llvm/lib/SandboxIR/Region.cpp index b14c87f44260f..b6292f3b24b87 100644 --- a/llvm/lib/SandboxIR/Region.cpp +++ b/llvm/lib/SandboxIR/Region.cpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #include "llvm/SandboxIR/Region.h" +#include "llvm/SandboxIR/Function.h" namespace llvm::sandboxir { diff --git a/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/BottomUpVec.cpp b/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/BottomUpVec.cpp index 7ebbcabb004df..c59abd09d4362 100644 --- a/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/BottomUpVec.cpp +++ b/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/BottomUpVec.cpp @@ -8,6 +8,7 @@ #include "llvm/Transforms/Vectorize/SandboxVectorizer/Passes/BottomUpVec.h" #include "llvm/ADT/SmallVector.h" +#include "llvm/SandboxIR/Function.h" #include "llvm/SandboxIR/Instruction.h" using namespace llvm::sandboxir; diff --git a/llvm/unittests/SandboxIR/PassTest.cpp b/llvm/unittests/SandboxIR/PassTest.cpp index 9cd54352735dc..10fe59b654a2e 100644 --- a/llvm/unittests/SandboxIR/PassTest.cpp +++ b/llvm/unittests/SandboxIR/PassTest.cpp @@ -11,6 +11,7 @@ #include "llvm/IR/Module.h" #include "llvm/SandboxIR/Constant.h" #include "llvm/SandboxIR/Context.h" +#include "llvm/SandboxIR/Function.h" #include "llvm/SandboxIR/PassManager.h" #include "llvm/Support/SourceMgr.h" #include "gtest/gtest.h" diff --git a/llvm/unittests/SandboxIR/RegionTest.cpp b/llvm/unittests/SandboxIR/RegionTest.cpp index f1bb535d9c50e..602534530e248 100644 --- a/llvm/unittests/SandboxIR/RegionTest.cpp +++ b/llvm/unittests/SandboxIR/RegionTest.cpp @@ -8,8 +8,8 @@ #include "llvm/SandboxIR/Region.h" #include "llvm/AsmParser/Parser.h" -#include "llvm/SandboxIR/Constant.h" #include "llvm/SandboxIR/Context.h" +#include "llvm/SandboxIR/Function.h" #include "llvm/SandboxIR/Instruction.h" #include "llvm/Support/SourceMgr.h" #include "gmock/gmock-matchers.h" diff --git a/llvm/unittests/SandboxIR/SandboxIRTest.cpp b/llvm/unittests/SandboxIR/SandboxIRTest.cpp index 66a5191b1154b..3bd520f3174c2 100644 --- a/llvm/unittests/SandboxIR/SandboxIRTest.cpp +++ b/llvm/unittests/SandboxIR/SandboxIRTest.cpp @@ -15,6 +15,7 @@ #include "llvm/IR/Module.h" #include "llvm/SandboxIR/BasicBlock.h" #include "llvm/SandboxIR/Constant.h" +#include "llvm/SandboxIR/Function.h" #include "llvm/SandboxIR/Instruction.h" #include "llvm/SandboxIR/Module.h" #include "llvm/SandboxIR/Utils.h" diff --git a/llvm/unittests/SandboxIR/TrackerTest.cpp b/llvm/unittests/SandboxIR/TrackerTest.cpp index 5823f4e14a854..9c7710a6a9297 100644 --- a/llvm/unittests/SandboxIR/TrackerTest.cpp +++ b/llvm/unittests/SandboxIR/TrackerTest.cpp @@ -11,6 +11,7 @@ #include "llvm/IR/Function.h" #include "llvm/IR/Instruction.h" #include "llvm/IR/Module.h" +#include "llvm/SandboxIR/Function.h" #include "llvm/SandboxIR/Instruction.h" #include "llvm/Support/SourceMgr.h" #include "gmock/gmock-matchers.h" diff --git a/llvm/unittests/SandboxIR/TypesTest.cpp b/llvm/unittests/SandboxIR/TypesTest.cpp index 9bf02c97948eb..6ccd08d4e710f 100644 --- a/llvm/unittests/SandboxIR/TypesTest.cpp +++ b/llvm/unittests/SandboxIR/TypesTest.cpp @@ -16,6 +16,7 @@ #include "llvm/IR/Module.h" #include "llvm/SandboxIR/Constant.h" #include "llvm/SandboxIR/Context.h" +#include "llvm/SandboxIR/Function.h" #include "llvm/Support/SourceMgr.h" #include "gtest/gtest.h" diff --git a/llvm/unittests/SandboxIR/UtilsTest.cpp b/llvm/unittests/SandboxIR/UtilsTest.cpp index 18d62d95d2433..96c58ba2ee532 100644 --- a/llvm/unittests/SandboxIR/UtilsTest.cpp +++ b/llvm/unittests/SandboxIR/UtilsTest.cpp @@ -15,6 +15,7 @@ #include "llvm/IR/Module.h" #include "llvm/SandboxIR/Constant.h" #include "llvm/SandboxIR/Context.h" +#include "llvm/SandboxIR/Function.h" #include "llvm/Support/SourceMgr.h" #include "gtest/gtest.h" diff --git a/llvm/unittests/Transforms/Vectorize/SandboxVectorizer/DependencyGraphTest.cpp b/llvm/unittests/Transforms/Vectorize/SandboxVectorizer/DependencyGraphTest.cpp index 329d3617a31fa..d8b6f519982eb 100644 --- a/llvm/unittests/Transforms/Vectorize/SandboxVectorizer/DependencyGraphTest.cpp +++ b/llvm/unittests/Transforms/Vectorize/SandboxVectorizer/DependencyGraphTest.cpp @@ -8,8 +8,8 @@ #include "llvm/Transforms/Vectorize/SandboxVectorizer/DependencyGraph.h" #include "llvm/AsmParser/Parser.h" -#include "llvm/SandboxIR/Constant.h" #include "llvm/SandboxIR/Context.h" +#include "llvm/SandboxIR/Function.h" #include "llvm/SandboxIR/Instruction.h" #include "llvm/Support/SourceMgr.h" #include "gmock/gmock-matchers.h" diff --git a/llvm/unittests/Transforms/Vectorize/SandboxVectorizer/IntervalTest.cpp b/llvm/unittests/Transforms/Vectorize/SandboxVectorizer/IntervalTest.cpp index 0b2411151a965..d463a61d5969b 100644 --- a/llvm/unittests/Transforms/Vectorize/SandboxVectorizer/IntervalTest.cpp +++ b/llvm/unittests/Transforms/Vectorize/SandboxVectorizer/IntervalTest.cpp @@ -8,8 +8,8 @@ #include "llvm/Transforms/Vectorize/SandboxVectorizer/Interval.h" #include "llvm/AsmParser/Parser.h" -#include "llvm/SandboxIR/Constant.h" #include "llvm/SandboxIR/Context.h" +#include "llvm/SandboxIR/Function.h" #include "llvm/SandboxIR/Instruction.h" #include "llvm/Support/SourceMgr.h" #include "gtest/gtest.h" diff --git a/llvm/unittests/Transforms/Vectorize/SandboxVectorizer/LegalityTest.cpp b/llvm/unittests/Transforms/Vectorize/SandboxVectorizer/LegalityTest.cpp index 89255a108ed6c..e16222ddb2d61 100644 --- a/llvm/unittests/Transforms/Vectorize/SandboxVectorizer/LegalityTest.cpp +++ b/llvm/unittests/Transforms/Vectorize/SandboxVectorizer/LegalityTest.cpp @@ -8,6 +8,7 @@ #include "llvm/Transforms/Vectorize/SandboxVectorizer/Legality.h" #include "llvm/AsmParser/Parser.h" +#include "llvm/SandboxIR/Function.h" #include "llvm/SandboxIR/Instruction.h" #include "llvm/Support/SourceMgr.h" #include "gtest/gtest.h"