Finished support code; now debugging why LLVM crashes during codegen

This commit is contained in:
2022-05-26 14:17:51 +02:00
parent c90cc0a9bc
commit 4c2dc2284a
2 changed files with 79 additions and 14 deletions

View File

@@ -5,12 +5,20 @@ set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED True)
include(FindPkgConfig)
find_package(LLVM 13 REQUIRED CONFIG)
find_package(Boost 1.78
COMPONENTS log)
list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR}")
include(HandleLLVMOptions)
if (EXISTS "${CMAKE_CURRENT_LIST_DIR}/vendor/llvm/CMakeLists.txt")
message(STATUS "Using vendored LLVM")
set(LLVM_TARGETS_TO_BUILD "X86;ARM;AArch64")
set(LLVM_INCLUDE_BENCHMARKS NO)
add_subdirectory(vendor/llvm)
else()
message(STATUS "Using system LLVM")
find_package(LLVM 13 REQUIRED CONFIG)
include(AddLLVM)
include(HandleLLVMOptions)
endif()
#pkg_check_modules(shaderc REQUIRED IMPORTED_TARGET shaderc)

View File

@@ -1078,8 +1078,9 @@ public:
void generate_support() {
generate_support_setup_frame();
generate_support_get_context_size();
generate_support_render_frame();
generate_support_render_pixel();
}
private:
void generate_support_get_context_size() const {
auto ty_fn = llvm::FunctionType::get(llvm::Type::getInt32Ty(*ctx), false);
auto fn = llvm::Function::Create(
@@ -1147,10 +1148,66 @@ public:
}
void generate_support_render_pixel() {
auto floatTy = llvm::Type::getFloatTy(*ctx);
auto v4f_ty = llvm::VectorType::get(floatTy, 4, false);
auto a3f_ty = llvm::ArrayType::get(floatTy, 3);
std::vector<llvm::Type *> fn_args = {
global_type->getPointerTo(), // globals
floatTy, floatTy, // x,y
a3f_ty->getPointerTo(), // output color
};
auto fn_ty = llvm::FunctionType::get(llvm::Type::getVoidTy(*ctx), fn_args, false);
auto function = llvm::Function::Create(fn_ty, llvm::Function::ExternalLinkage, "render_pixel", *module);
auto bb = llvm::BasicBlock::Create(*ctx, "entry", function);
builder->SetInsertPoint(bb);
void generate_support_render_frame() {
auto shader_ctx = function->getArg(0);
auto x = function->getArg(1);
auto y = function->getArg(2);
auto result_buf = function->getArg(3);
// find the relevant globals
int fragCoordPos = -1;
int fragColorPos = -1;
for (auto &global_it: globals) {
uint32_t dval;
if (get_decoration(global_it.first, spv::Decoration::Location, dval) && dval == 0) {
fragColorPos = (int)global_it.second.element_no;
} else if (get_decoration(global_it.first, spv::Decoration::BuiltIn, dval) && dval == (uint32_t)spv::BuiltIn::FragCoord) {
fragCoordPos = (int)global_it.second.element_no;
}
}
if (fragCoordPos != -1) {
auto fc_ptr = builder->CreateStructGEP(global_type, shader_ctx, (unsigned )fragCoordPos, "gl_fragCoord");
llvm::Value* fc_gen = llvm::ConstantVector::getSplat(llvm::ElementCount::get(4, false), llvm::ConstantFP::get(floatTy, 1.0));
fc_gen = builder->CreateInsertElement(fc_gen, x, (uint64_t)0);
fc_gen = builder->CreateInsertElement(fc_gen, y, (uint64_t)1);
builder->CreateStore(fc_gen, fc_ptr);
} else {
BOOST_LOG_TRIVIAL(warning) << "Shader doesn't have use gl_FragCoord";
}
builder->CreateCall(module->getFunction("sh_main"), shader_ctx);
if (fragColorPos != -1) {
auto colorV = builder->CreateLoad(v4f_ty, builder->CreateStructGEP(global_type, shader_ctx, (unsigned) fragColorPos));
for (int i = 0; i < 3; i++) {
auto channel_ptr = builder->CreateConstGEP2_32(a3f_ty, result_buf, 0, i);
auto channel_v = builder->CreateExtractElement(colorV, i);
builder->CreateStore(channel_v, channel_ptr);
}
} else {
BOOST_LOG_TRIVIAL(warning) << "Shader doesn't have a fragColor";
auto const0 = llvm::ConstantFP::get(floatTy , 0);
for (int i = 0; i < 3; i++) {
auto channel_ptr = builder->CreateConstGEP2_32(a3f_ty, result_buf, 0, i);
builder->CreateStore(const0, channel_ptr);
}
}
builder->CreateRetVoid();
}
private:
@@ -1244,7 +1301,7 @@ llvm::Optional<llvm::orc::ThreadSafeModule> Compiler::compile(std::vector<uint32
llvm::verifyModule(*impl->module);
// impl->module->print(llvm::outs(), nullptr, false, true);
impl->module->print(llvm::outs(), nullptr, false, true);
BOOST_LOG_TRIVIAL(debug) << "Optimizing";
// start generating machine code
@@ -1268,15 +1325,15 @@ llvm::Optional<llvm::orc::ThreadSafeModule> Compiler::compile(std::vector<uint32
llvm::PassManagerBuilder pass_builder;
target_machine->adjustPassManager(pass_builder);
llvm::legacy::PassManager pass;
pass.add(llvm::createArgumentPromotionPass());
pass.add(llvm::createPromoteMemoryToRegisterPass());
pass.add(llvm::createInstructionCombiningPass());
pass.add(llvm::createReassociatePass());
pass.add(llvm::createGVNPass());
pass.add(llvm::createCFGSimplificationPass());
pass.add(llvm::createFunctionInliningPass(3, 0, false));
pass.add(llvm::createAggressiveDCEPass());
pass.add(llvm::createLICMPass());
// pass.add(llvm::createArgumentPromotionPass());
// pass.add(llvm::createPromoteMemoryToRegisterPass());
// pass.add(llvm::createInstructionCombiningPass());
// pass.add(llvm::createReassociatePass());
// pass.add(llvm::createGVNPass());
// pass.add(llvm::createCFGSimplificationPass());
// pass.add(llvm::createFunctionInliningPass(3, 0, false));
// pass.add(llvm::createAggressiveDCEPass());
// pass.add(llvm::createLICMPass());
pass_builder.populateModulePassManager(pass);
// add output to object file