Skip to content
Open
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
13 changes: 12 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.5)
cmake_minimum_required(VERSION 3.15)

project(SysYFCompiler)

Expand All @@ -23,6 +23,15 @@ else()
message(WARNING "Unknown build type: ${CMAKE_BUILD_TYPE}. Using default flags.")
endif()

include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

# for ninja color, see https://stackoverflow.com/questions/73349743/ninja-build-system-gcc-clang-doesnt-output-diagnostic-colors
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fdiagnostics-color=always")

Expand All @@ -43,6 +52,8 @@ add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/src/SysYFIR)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/src/SysYFIRBuilder)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/Student/task2)

add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/unittest)

add_executable(
SysYFCompiler
${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp
Expand Down
2 changes: 1 addition & 1 deletion Student/task2/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
project(task2)

cmake_minimum_required(VERSION 3.5)
cmake_minimum_required(VERSION 3.15)

set(CMAKE_CXX_STANDARD 14)

Expand Down
1 change: 0 additions & 1 deletion include/SysYFIR/User.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class User : public Value

private:
std::vector<std::weak_ptr<Value>> operands_; // operands of this value
unsigned num_ops_;
};

}
Expand Down
26 changes: 16 additions & 10 deletions src/SysYFIR/User.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "User.h"
#include "internal_types.h"
#include <memory>
#include <vector>
#ifdef DEBUG
#include <cassert>
#endif
Expand All @@ -10,11 +11,11 @@ namespace SysYF
namespace IR
{
User::User(Ptr<Type> ty, const std::string &name , unsigned num_ops)
: Value(ty, name), num_ops_(num_ops)
: Value(ty, name)
{
// if (num_ops_ > 0)
// operands_.reset(new std::list<Ptr<Value> >());
operands_.resize(num_ops_);
operands_.resize(num_ops);
}

Ptr<Value> User::get_operand(unsigned i) const
Expand All @@ -35,13 +36,12 @@ void User::set_operand(unsigned i, Ptr<Value> v)
void User::add_operand(Ptr<Value> v)
{
operands_.push_back(v);
v->add_use(shared_from_this(), num_ops_);
num_ops_++;
v->add_use(shared_from_this(), operands_.size()-1);
}

unsigned User::get_num_operand() const
{
return num_ops_;
return operands_.size();
}

void User::remove_use_of_ops()
Expand All @@ -51,13 +51,19 @@ void User::remove_use_of_ops()
}
}

void User::remove_operands(int index1,int index2){
for(int i=index1;i<=index2;i++){
operands_[i].lock()->remove_use(shared_from_this());
void User::remove_operands(int index1, int index2) {
auto backup = std::vector<std::weak_ptr<Value>>{};
for (auto iter = operands_.cbegin() + index2 + 1; iter != operands_.cend(); iter++) {
backup.push_back(*iter);
}
for (auto iter = operands_.cbegin() + index1; iter != operands_.cend(); iter++) {
iter->lock()->remove_use(shared_from_this());
}
operands_.erase(operands_.begin()+index1, operands_.end());
for (auto op: backup) {
add_operand(op.lock());
}
operands_.erase(operands_.begin()+index1,operands_.begin()+index2+1);
// std::cout<<operands_.size()<<std::endl;
num_ops_=operands_.size();
}

}
Expand Down
15 changes: 15 additions & 0 deletions unittest/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
enable_testing()

add_executable(
ir_test
ir_test.cpp
)

target_link_libraries(
ir_test
GTest::gtest_main
IRLib
)

include(GoogleTest)
gtest_discover_tests(ir_test)
27 changes: 27 additions & 0 deletions unittest/ir_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <gtest/gtest.h>
#include <iostream>

#include "Constant.h"
#include "Function.h"
#include "Instruction.h"
#include "Module.h"
#include "Type.h"

using namespace SysYF::IR;

TEST(IRTest, UserRemoveOperands) {
// https://github.com/ustc-compiler/IRGen/issues/2
std::cout << "user remove operands";
auto m = Module::create("test");
auto fn = Function::create(FunctionType::create(Type::create(SysYF::IR::Type::VoidTyID, m), {}, m), "foo", m);
auto bb = BasicBlock::create(m, "entry", fn);
auto const_1 = ConstantInt::create(1, m);
auto const_2 = ConstantInt::create(2, m);
auto add = BinaryInst::create_add(const_1, const_2, bb, m);
add->remove_operands(0, 0); // remove the first operand
for (unsigned int i = 0; i < add->get_num_operand(); i++) {
auto op = add->get_operand(i);
ASSERT_EQ(op->get_use_list().front().arg_no_, i);
}
}