Skip to content

[SandboxIR][NFC] Move Function class to a separate file #110526

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions llvm/benchmarks/SandboxIRBench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
53 changes: 0 additions & 53 deletions llvm/include/llvm/SandboxIR/Constant.h
Original file line number Diff line number Diff line change
Expand Up @@ -1227,59 +1227,6 @@ class ConstantTokenNone final : public Constant {
#endif
};

class Function : public GlobalWithNodeAPI<Function, llvm::Function,
GlobalObject, llvm::GlobalObject> {
/// Helper for mapped_iterator.
struct LLVMBBToBB {
Context &Ctx;
LLVMBBToBB(Context &Ctx) : Ctx(Ctx) {}
BasicBlock &operator()(llvm::BasicBlock &LLVMBB) const {
return *cast<BasicBlock>(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<llvm::Function>(Val)->getParent());
}

Argument *getArg(unsigned Idx) const {
llvm::Argument *Arg = cast<llvm::Function>(Val)->getArg(Idx);
return cast<Argument>(Ctx.getValue(Arg));
}

size_t arg_size() const { return cast<llvm::Function>(Val)->arg_size(); }
bool arg_empty() const { return cast<llvm::Function>(Val)->arg_empty(); }

using iterator = mapped_iterator<llvm::Function::iterator, LLVMBBToBB>;
iterator begin() const {
LLVMBBToBB BBGetter(Ctx);
return iterator(cast<llvm::Function>(Val)->begin(), BBGetter);
}
iterator end() const {
LLVMBBToBB BBGetter(Ctx);
return iterator(cast<llvm::Function>(Val)->end(), BBGetter);
}
FunctionType *getFunctionType() const;

#ifndef NDEBUG
void verify() const final {
assert(isa<llvm::Function>(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
72 changes: 72 additions & 0 deletions llvm/include/llvm/SandboxIR/Function.h
Original file line number Diff line number Diff line change
@@ -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<Function, llvm::Function,
GlobalObject, llvm::GlobalObject> {
/// Helper for mapped_iterator.
struct LLVMBBToBB {
Context &Ctx;
LLVMBBToBB(Context &Ctx) : Ctx(Ctx) {}
BasicBlock &operator()(llvm::BasicBlock &LLVMBB) const {
return *cast<BasicBlock>(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<llvm::Function>(Val)->getParent());
}

Argument *getArg(unsigned Idx) const {
llvm::Argument *Arg = cast<llvm::Function>(Val)->getArg(Idx);
return cast<Argument>(Ctx.getValue(Arg));
}

size_t arg_size() const { return cast<llvm::Function>(Val)->arg_size(); }
bool arg_empty() const { return cast<llvm::Function>(Val)->arg_empty(); }

using iterator = mapped_iterator<llvm::Function::iterator, LLVMBBToBB>;
iterator begin() const {
LLVMBBToBB BBGetter(Ctx);
return iterator(cast<llvm::Function>(Val)->begin(), BBGetter);
}
iterator end() const {
LLVMBBToBB BBGetter(Ctx);
return iterator(cast<llvm::Function>(Val)->end(), BBGetter);
}
FunctionType *getFunctionType() const;

#ifndef NDEBUG
void verify() const final {
assert(isa<llvm::Function>(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
1 change: 1 addition & 0 deletions llvm/lib/SandboxIR/BasicBlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/SandboxIR/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ add_llvm_component_library(LLVMSandboxIR
BasicBlock.cpp
Constant.cpp
Context.cpp
Function.cpp
Instruction.cpp
Module.cpp
Pass.cpp
Expand Down
41 changes: 1 addition & 40 deletions llvm/lib/SandboxIR/Constant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -467,44 +468,4 @@ GlobalValue *DSOLocalEquivalent::getGlobalValue() const {
Ctx.getValue(cast<llvm::DSOLocalEquivalent>(Val)->getGlobalValue()));
}

FunctionType *Function::getFunctionType() const {
return cast<FunctionType>(
Ctx.getType(cast<llvm::Function>(Val)->getFunctionType()));
}

#ifndef NDEBUG
void Function::dumpNameAndArgs(raw_ostream &OS) const {
auto *F = cast<llvm::Function>(Val);
OS << *F->getReturnType() << " @" << F->getName() << "(";
interleave(
F->args(),
[this, &OS](const llvm::Argument &LLVMArg) {
auto *SBArg = cast_or_null<Argument>(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<llvm::Function>(Val);
interleave(
*LLVMF,
[this, &OS](const llvm::BasicBlock &LLVMBB) {
auto *BB = cast_or_null<BasicBlock>(Ctx.getValue(&LLVMBB));
if (BB == nullptr)
OS << "NULL";
else
OS << *BB;
},
[&OS] { OS << "\n"; });
OS << "}\n";
}
#endif // NDEBUG

} // namespace llvm::sandboxir
1 change: 1 addition & 0 deletions llvm/lib/SandboxIR/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

#include "llvm/SandboxIR/Context.h"
#include "llvm/SandboxIR/Function.h"
#include "llvm/SandboxIR/Instruction.h"
#include "llvm/SandboxIR/Module.h"

Expand Down
55 changes: 55 additions & 0 deletions llvm/lib/SandboxIR/Function.cpp
Original file line number Diff line number Diff line change
@@ -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<FunctionType>(
Ctx.getType(cast<llvm::Function>(Val)->getFunctionType()));
}

#ifndef NDEBUG
void Function::dumpNameAndArgs(raw_ostream &OS) const {
auto *F = cast<llvm::Function>(Val);
OS << *F->getReturnType() << " @" << F->getName() << "(";
interleave(
F->args(),
[this, &OS](const llvm::Argument &LLVMArg) {
auto *SBArg = cast_or_null<Argument>(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<llvm::Function>(Val);
interleave(
*LLVMF,
[this, &OS](const llvm::BasicBlock &LLVMBB) {
auto *BB = cast_or_null<BasicBlock>(Ctx.getValue(&LLVMBB));
if (BB == nullptr)
OS << "NULL";
else
OS << *BB;
},
[&OS] { OS << "\n"; });
OS << "}\n";
}
#endif // NDEBUG

} // namespace llvm::sandboxir
1 change: 1 addition & 0 deletions llvm/lib/SandboxIR/Instruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

#include "llvm/SandboxIR/Instruction.h"
#include "llvm/SandboxIR/Function.h"

namespace llvm::sandboxir {

Expand Down
1 change: 1 addition & 0 deletions llvm/lib/SandboxIR/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/SandboxIR/Region.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

#include "llvm/SandboxIR/Region.h"
#include "llvm/SandboxIR/Function.h"

namespace llvm::sandboxir {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions llvm/unittests/SandboxIR/PassTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion llvm/unittests/SandboxIR/RegionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions llvm/unittests/SandboxIR/SandboxIRTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions llvm/unittests/SandboxIR/TrackerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions llvm/unittests/SandboxIR/TypesTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
1 change: 1 addition & 0 deletions llvm/unittests/SandboxIR/UtilsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading