Skip to content

Commit

Permalink
Added static inlining
Browse files Browse the repository at this point in the history
  • Loading branch information
techie-mike committed Apr 16, 2024
1 parent fec24d1 commit 8f91d90
Show file tree
Hide file tree
Showing 11 changed files with 652 additions and 30 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ set(COMPILER_SOURCES
${CMAKE_SOURCE_DIR}/src/optimizations/linear_scan.cpp
${CMAKE_SOURCE_DIR}/src/optimizations/peepholes.cpp
${CMAKE_SOURCE_DIR}/src/optimizations/constant_folding.cpp
${CMAKE_SOURCE_DIR}/src/optimizations/inlining.cpp
)

add_library(CompilerLibBase ${COMPILER_SOURCES})
Expand Down
32 changes: 31 additions & 1 deletion src/graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ std::string Graph::GetMethodName() const
return name_method_;
}

void Graph::SetNumParams(uint32_t num) {
num_params_ = num;
}
uint32_t Graph::GetNumParams() {
return num_params_;
}

Inst *Graph::CreateClearInstByOpcode(Opcode opc) {
switch(opc) {

Expand Down Expand Up @@ -100,7 +107,7 @@ void Graph::DumpDomTree(std::ostream &out) {
}
}

void Graph::DumpPlacedInsts(std::ostream &out) {
void Graph::DumpPlacedInsts(std::ostream &out) const {
bool first = true;
out << "Instructions is PLACED:" << std::endl;
for (auto region : all_regions_) {
Expand All @@ -116,6 +123,29 @@ void Graph::DumpPlacedInsts(std::ostream &out) {
}
}

void Graph::AddInst(Inst *inst) {
all_inst_.push_back(inst);
}

void Graph::DeleteInst(Inst *inst) {
// Delete all inputs
for (id_t i = 0; i < inst->NumAllInputs(); i++) {
if (inst->GetRawInput(i) != nullptr) {
inst->GetRawInput(i)->DeleteRawUser(inst);
}
}
// Delete all users
if (inst->NumDataUsers() > 0) {
for (auto it = inst->GetRawUsers().begin(); it != inst->GetRawUsers().end(); it = (*it == nullptr) ? ++it : inst->GetRawUsers().begin()) {
if (*it != nullptr) {
(*it)->DeleteInput(inst);
inst->DeleteRawUser(*it);
}
}
}
auto del = std::find(all_inst_.begin(), all_inst_.end(), inst);
*del = nullptr;
deleted_insts_.push_back(inst);
}

}
19 changes: 16 additions & 3 deletions src/graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,24 @@ class Graph
}
delete inst;
}
for (auto inst : deleted_insts_) {
if (inst == nullptr) {
continue;
}
delete inst;
}
}

void SetMethodName(const std::string& name);
std::string GetMethodName() const;

void SetNumParams(uint32_t num);
uint32_t GetNumParams();

void Dump(std::ostream &out);
void DumpDomTree(std::ostream &out);
void AddInst(Inst *inst);
void DeleteInst(Inst *inst);

#define CREATE_CREATORS(OPCODE, BASE) \
template <typename... Args> \
Expand Down Expand Up @@ -89,7 +100,7 @@ class Graph
unit_test_mode_ = true;
}

size_t GetNumInsts() {
size_t GetNumInsts() const {
return all_inst_.size();
}

Expand Down Expand Up @@ -117,7 +128,7 @@ class Graph
root_loop_ = loop;
}

void DumpPlacedInsts(std::ostream &out);
void DumpPlacedInsts(std::ostream &out) const;

RegionInst *GetStartRegion() {
return GetInstByIndex(0)->CastToRegion();
Expand All @@ -127,18 +138,20 @@ class Graph
insts_placed_ = true;
}

bool IsInstsPlaced() {
bool IsInstsPlaced() const {
return insts_placed_;
}

private:
bool unit_test_mode_ = false;
bool insts_placed_ = false;
uint32_t num_loops_ = 0;
uint32_t num_params_ = 0;
Loop *root_loop_ = nullptr;
std::string name_method_;
std::vector<Inst *> all_inst_;
std::vector<RegionInst *> all_regions_;
std::vector<Inst *> deleted_insts_;
};

}
134 changes: 126 additions & 8 deletions src/inst.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ void Inst::SetDataInput(id_t index, Inst *inst) {

Inst *Inst::GetDataInput(id_t index) {
ASSERT(!IsRegion()); // RegionInst has special method GetRegionInput
ASSERT(index < NumDataInputs());
if (HasControlProp()) {
index++;
}
Expand Down Expand Up @@ -77,6 +78,13 @@ void Inst::DeleteDataUser(Inst *inst) {
users_.erase(it);
}

void Inst::DeleteRawUser(Inst *inst) {
auto it = std::find(GetRawUsers().begin(), GetRawUsers().end(), inst);
if (it != GetRawUsers().end()) {
users_.erase(it);
}
}

uint32_t Inst::NumDataUsers() {
return HasControlProp() ? GetRawUsers().size() - 1 : GetRawUsers().size();
}
Expand Down Expand Up @@ -193,15 +201,15 @@ void PhiInst::DumpInputs(std::ostream &out) {
}

void DynamicInputs::DumpInputs(std::ostream &out) {
bool first = true;
for (auto inst : GetAllInputs()) {
if (inst == nullptr) {
continue;
}
out << std::string(first ? "" : ", ") << std::string("v") << std::to_string(inst->GetId());
first = false;
bool first = true;
for (auto inst : GetAllInputs()) {
if (inst == nullptr) {
continue;
}
out << std::string(first ? "" : ", ") << std::string("v") << std::to_string(inst->GetId());
first = false;
}
}

std::string OpcodeToString(Opcode opc) {
return OPCODE_NAME.at(static_cast<size_t>(opc));
Expand Down Expand Up @@ -270,10 +278,27 @@ ConstantInst *Inst::CastToConstant() {
return static_cast<ConstantInst *>(this);
}

ParameterInst *Inst::CastToParameter() {
ASSERT(GetOpcode() == Opcode::Parameter);
return static_cast<ParameterInst *>(this);
}

CallInst *Inst::CastToCall() {
ASSERT(GetOpcode() == Opcode::Call);
return static_cast<CallInst *>(this);
}

JumpInst *Inst::CastToJump() {
ASSERT(GetOpcode() == Opcode::Jump);
return static_cast<JumpInst *>(this);
}

void Inst::ReplaceDataUsers(Inst *from) {
ASSERT(!HasControlProp());
for (auto it = from->StartIteratorDataUsers(); it != from->GetRawUsers().end(); it = from->StartIteratorDataUsers()) {
auto user = *it;
if (user == nullptr) {
continue;
}
for (id_t i = 0; i < user->NumDataInputs(); i++) {
if (user->GetDataInput(i) == from) {
user->SetDataInput(i, this);
Expand All @@ -283,4 +308,97 @@ void Inst::ReplaceDataUsers(Inst *from) {
from->GetRawUsers().erase(from->StartIteratorDataUsers(), from->GetRawUsers().end());
}

void CallInst::SetNameFunc(const std::string &name) {
name_func_ = name;
}

std::string CallInst::GetNameFunc() const {
return name_func_;
}

Inst *Inst::LiteClone(Graph *target_graph, std::map<id_t, id_t> &connect) {
auto new_inst = target_graph->CreateClearInstByOpcode(GetOpcode());
new_inst->type_ = type_;
new_inst->SetId(target_graph->GetNumInsts());
target_graph->AddInst(new_inst);

// To many specific cases in common code!
auto opc = GetOpcode();
if (opc == Opcode::Start || opc == Opcode::Parameter || opc == Opcode::Region || opc == Opcode::End) {
return new_inst;
}
// if (opc == Opcode::Region || opc == Opcode::End) {
// auto num_inputs = CastToRegion()->NumRegionInputs();
// for (id_t index = 0; index < num_inputs; index++) {
// new_inst->CastToRegion()->SetRegionInput(index, target_graph->GetInstByIndex(connect[CastToRegion()->GetRegionInput(index)->GetId()]));
// }
// return new_inst;
// }

if (HasControlProp() && GetControlInput() != nullptr) {
new_inst->SetControlInput(target_graph->GetInstByIndex(connect[GetControlInput()->GetId()]));
}
auto num_inputs = NumDataInputs();
for (id_t index = 0; index < num_inputs; index++) {
// If id_t > num_all_inst in graph, write input in order (case for Jump and If)
new_inst->SetDataInput(index, target_graph->GetInstByIndex(connect[GetDataInput(index)->GetId()]));
}
return new_inst;
}

Inst *ConstantInst::LiteClone(Graph *target_graph, std::map<id_t, id_t> &connect) {
auto new_inst = static_cast<ConstantInst *>(Inst::LiteClone(target_graph, connect));
new_inst->SetImm(GetImm());
return new_inst;
}

Inst *CompareInst::LiteClone(Graph *target_graph, std::map<id_t, id_t> &connect) {
auto new_inst = static_cast<CompareInst *>(FixedInputs<2>::LiteClone(target_graph, connect));
new_inst->SetCC(GetCC());
return new_inst;
}

Inst *ParameterInst::LiteClone(Graph *target_graph, std::map<id_t, id_t> &connect) {
auto new_inst = static_cast<ParameterInst *>(Inst::LiteClone(target_graph, connect));
new_inst->SetIndexParam(GetIndexParam());
return new_inst;
}

Inst *CallInst::LiteClone(Graph *target_graph, std::map<id_t, id_t> &connect) {
auto new_inst = static_cast<CallInst *>(Inst::LiteClone(target_graph, connect));
new_inst->SetNameFunc(GetNameFunc());
return new_inst;
}

void CallInst::DumpInputs(std::ostream &out) {
out << "\"" << GetNameFunc() << "\" ";
Base::DumpInputs(out);
}

void ParameterInst::DumpInputs(std::ostream &out) {
out << "\"" << GetIndexParam() << "\" ";
Inst::DumpInputs(out);
}

void Inst::ReplaceAllUsers(Inst *from) {
ReplaceDataUsers(from);
ReplaceCtrUser(from);
}

void Inst::ReplaceCtrUser(Inst *from) {
ASSERT(HasSingleDataUser());
auto c_user = from->GetControlUser();
if (c_user == nullptr) {
return;
}
SetControlUser(c_user);
from->SetControlUser(nullptr);
}

void DynamicInputs::DeleteInput(Inst *inst) {
auto it = std::find(inputs_.begin(), inputs_.end(), inst);
ASSERT(it != inputs_.end());
inputs_.erase(it);
}

}
Loading

0 comments on commit 8f91d90

Please sign in to comment.