Skip to content

Commit fd0eedb

Browse files
youbenyweimingzha0
authored andcommitted
[IR] add argument/constant for BasicBlock class
1 parent 0ae168d commit fd0eedb

File tree

6 files changed

+70
-20
lines changed

6 files changed

+70
-20
lines changed

Diff for: include/halo/lib/ir/argument.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ class Argument final : public IRObject {
3939
explicit Argument(GlobalContext& context, const std::string& name,
4040
const Type& type);
4141

42-
/// Get the parent function of this argument.
43-
Function* GetParent() const noexcept { return parent_function_; }
42+
/// Returns the parent object that could be a Function or a BasicBlock.
43+
IRObject* GetParent() const noexcept { return parent_; }
4444

4545
/// Set the type of this argument.
4646
void SetType(const Type& type);
@@ -55,11 +55,11 @@ class Argument final : public IRObject {
5555
void Print(std::ostream& os) const override;
5656

5757
private:
58-
Function* parent_function_ = nullptr;
58+
IRObject* parent_ = nullptr;
5959

6060
friend class ArgumentBuilder;
6161
};
6262

6363
} // namespace halo
6464

65-
#endif // HALO_LIB_IR_ARGUMENT_H_
65+
#endif // HALO_LIB_IR_ARGUMENT_H_

Diff for: include/halo/lib/ir/basic_block.h

+37-3
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@
2020

2121
#include <list>
2222

23+
#include "halo/lib/ir/argument.h"
24+
#include "halo/lib/ir/basic_block.h"
25+
#include "halo/lib/ir/constant.h"
2326
#include "halo/lib/ir/instruction.h"
2427

2528
namespace halo {
2629

30+
class Argument;
2731
class Function;
2832

2933
/// This class defines a basic block in IR.
@@ -77,8 +81,36 @@ class BasicBlock final : public IRObject {
7781
InstructionList& Instructions() noexcept { return instructions_; }
7882
const InstructionList& Instructions() const noexcept { return instructions_; }
7983

84+
/// Arguments
85+
using ArgumentList = std::list<std::unique_ptr<Argument>>;
86+
using arg_iterator = ArgumentList::iterator;
87+
using const_arg_iterator = ArgumentList::const_iterator;
88+
arg_iterator arg_begin() noexcept { return args_.begin(); }
89+
const_arg_iterator arg_begin() const noexcept { return args_.begin(); }
90+
arg_iterator arg_end() noexcept { return args_.end(); }
91+
const_arg_iterator arg_end() const noexcept { return args_.end(); }
92+
ArgumentList& Args() noexcept { return args_; }
93+
const ArgumentList& Args() const noexcept { return args_; }
94+
Argument* arg_front() const { return args_.front().get(); }
95+
Argument* arg_back() const { return args_.back().get(); }
96+
97+
/// Constants
98+
using ConstantList = std::list<std::unique_ptr<Constant>>;
99+
using constant_iterator = ConstantList::iterator;
100+
using const_constant_iterator = ConstantList::const_iterator;
101+
constant_iterator constant_begin() noexcept { return constants_.begin(); }
102+
const_constant_iterator constant_begin() const noexcept {
103+
return constants_.begin();
104+
}
105+
constant_iterator constant_end() noexcept { return constants_.end(); }
106+
const_constant_iterator constant_end() const noexcept {
107+
return constants_.end();
108+
}
109+
ConstantList& Constants() noexcept { return constants_; }
110+
const ConstantList& Constants() const noexcept { return constants_; }
111+
80112
/// Return the parent function to which this basic block belongs.
81-
Function* GetParent() noexcept { return parent_function_; }
113+
Function* GetParent() noexcept { return parent_; }
82114

83115
Kind GetKind() const noexcept override { return Kind::BasicBlock; }
84116

@@ -91,12 +123,14 @@ class BasicBlock final : public IRObject {
91123
void Print(std::ostream& os) const override;
92124

93125
private:
94-
Function* parent_function_ = nullptr;
126+
Function* parent_ = nullptr;
95127
InstructionList instructions_;
128+
ArgumentList args_;
129+
ConstantList constants_;
96130

97131
friend class BasicBlockBuilder;
98132
};
99133

100134
} // namespace halo
101135

102-
#endif // HALO_LIB_IR_BASIC_BLOCK_H_
136+
#endif // HALO_LIB_IR_BASIC_BLOCK_H_

Diff for: include/halo/lib/ir/ir_builder.h

+7-2
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,13 @@ class FunctionBuilder final
154154

155155
/// This class provides the APIs to create arguments for the current function.
156156
class ArgumentBuilder final
157-
: public IRObjectBuilder<Function, Function::ArgumentList> {
157+
: public IRObjectBuilder<IRObject, Function::ArgumentList> {
158158
public:
159159
explicit ArgumentBuilder(Function* function)
160160
: IRObjectBuilder(function->GetGlobalContext(), function,
161161
function->Args()) {}
162+
explicit ArgumentBuilder(BasicBlock* bb)
163+
: IRObjectBuilder(bb->GetGlobalContext(), bb, bb->Args()) {}
162164

163165
/// Create a new argument and insert it to the end of current argument list.
164166
Argument* CreateArgument(const std::string& name);
@@ -189,6 +191,9 @@ class ConstantBuilder final
189191
explicit ConstantBuilder(Module* parent)
190192
: IRObjectBuilder(parent->GetGlobalContext(), parent,
191193
parent->Constants()) {}
194+
explicit ConstantBuilder(BasicBlock* parent)
195+
: IRObjectBuilder(parent->GetGlobalContext(), parent,
196+
parent->Constants()) {}
192197

193198
/// Create a new constant and append it to the end of current function or
194199
/// module using default data layout.
@@ -258,4 +263,4 @@ class IRBuilder final
258263

259264
} // namespace halo
260265

261-
#endif // HALO_LIB_IR_IR_BUILDER_H_
266+
#endif // HALO_LIB_IR_IR_BUILDER_H_

Diff for: lib/ir/basic_block.cc

+14-2
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,22 @@
2020
namespace halo {
2121

2222
void BasicBlock::Print(std::ostream& os) const {
23-
os << "BasicBlock: " << GetName() << "\n";
23+
os << "BasicBlock: " << GetName() << "(";
24+
int arg_idx = 0;
25+
for (auto& arg : Args()) {
26+
if (arg_idx++ != 0) {
27+
os << ", ";
28+
}
29+
arg->Print(os);
30+
}
31+
os << ")\n";
32+
33+
for (auto& c : Constants()) {
34+
c->Print(os);
35+
}
2436
for (const auto& inst : *this) {
2537
inst->Print(os);
2638
}
2739
}
2840

29-
} // namespace halo
41+
} // namespace halo

Diff for: lib/ir/function.cc

+5-6
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@
2222
namespace halo {
2323

2424
ReturnInst* Function::GetReturnInst() const {
25-
for (auto& bb : basic_blocks_) {
26-
for (auto& inst : *bb) {
27-
if (inst->GetOpCode() == OpCode::RETURN) {
28-
return DynCast<ReturnInst>(inst.get());
29-
}
25+
HLCHECK(!basic_blocks_.empty());
26+
for (auto& inst : *basic_blocks_.front()) {
27+
if (inst->GetOpCode() == OpCode::RETURN) {
28+
return DynCast<ReturnInst>(inst.get());
3029
}
3130
}
3231
return nullptr;
@@ -52,4 +51,4 @@ void Function::Print(std::ostream& os) const {
5251
}
5352
}
5453

55-
} // namespace halo
54+
} // namespace halo

Diff for: lib/ir/ir_builder.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Function* FunctionBuilder::CreateFunction(const std::string& name) {
3333

3434
BasicBlock* BasicBlockBuilder::CreateBasicBlock(const std::string& name) {
3535
auto bb = std::make_unique<BasicBlock>(GetContext(), name);
36-
bb->parent_function_ = GetParent();
36+
bb->parent_ = GetParent();
3737
return Insert(std::move(bb));
3838
}
3939

@@ -44,7 +44,7 @@ Argument* ArgumentBuilder::CreateArgument(const std::string& name) {
4444
Argument* ArgumentBuilder::CreateArgument(const std::string& name,
4545
const Type& type) {
4646
auto arg = std::make_unique<Argument>(GetContext(), name, type);
47-
arg->parent_function_ = GetParent();
47+
arg->parent_ = GetParent();
4848
return Insert(std::move(arg));
4949
}
5050

@@ -221,4 +221,4 @@ DummyInst* IRBuilder::CreateDummy(const std::string& name,
221221

222222
#include "halo/lib/ir/ir_builder.cc.inc"
223223

224-
} // namespace halo
224+
} // namespace halo

0 commit comments

Comments
 (0)