diff --git a/include/luisa/luisa-compute.h b/include/luisa/luisa-compute.h index dcae0893a..a3795d0ee 100644 --- a/include/luisa/luisa-compute.h +++ b/include/luisa/luisa-compute.h @@ -1,243 +1,245 @@ -#pragma once - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#ifdef LUISA_ENABLE_CLANGCXX -#include -#include -#endif - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#ifdef LUISA_ENABLE_DSL -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#endif - -#ifdef LUISA_ENABLE_GUI -#include -#include -#include -#include -#endif - -#ifdef LUISA_ENABLE_IR -#include -#include -#include -#include -#endif - -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#ifdef LUISA_ENABLE_RUST -#include -#include -#include -#include -#endif - -#ifdef LUISA_ENABLE_TENSOR -#include -#include -#endif - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#ifdef LUISA_ENABLE_XIR -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#endif - +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef LUISA_ENABLE_CLANGCXX +#include +#include +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef LUISA_ENABLE_DSL +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#endif + +#ifdef LUISA_ENABLE_GUI +#include +#include +#include +#include +#endif + +#ifdef LUISA_ENABLE_IR +#include +#include +#include +#include +#endif + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef LUISA_ENABLE_RUST +#include +#include +#include +#include +#endif + +#ifdef LUISA_ENABLE_TENSOR +#include +#include +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef LUISA_ENABLE_XIR +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#endif + diff --git a/include/luisa/xir/passes/sink_alloca.h b/include/luisa/xir/passes/sink_alloca.h new file mode 100644 index 000000000..77f08456c --- /dev/null +++ b/include/luisa/xir/passes/sink_alloca.h @@ -0,0 +1,17 @@ +#pragma once + +#include +#include + +namespace luisa::compute::xir { + +class AllocaInst; + +struct SinkAllocaInfo { + luisa::vector sunken_alloca_insts; +}; + +LC_XIR_API SinkAllocaInfo sink_alloca_pass_run_on_function(Module *module, Function *function) noexcept; +LC_XIR_API SinkAllocaInfo sink_alloca_pass_run_on_module(Module *module) noexcept; + +}// namespace luisa::compute::xir diff --git a/src/xir/CMakeLists.txt b/src/xir/CMakeLists.txt index 92b4bb18d..bb14d02fb 100644 --- a/src/xir/CMakeLists.txt +++ b/src/xir/CMakeLists.txt @@ -44,6 +44,7 @@ set(LUISA_COMPUTE_XIR_SOURCES # passes passes/outline.cpp + passes/sink_alloca.cpp ) add_library(luisa-compute-xir SHARED ${LUISA_COMPUTE_XIR_SOURCES}) diff --git a/src/xir/passes/outline.cpp b/src/xir/passes/outline.cpp index 6f035eac5..5173521a2 100644 --- a/src/xir/passes/outline.cpp +++ b/src/xir/passes/outline.cpp @@ -12,9 +12,8 @@ OutlineInfo outline_pass_run_on_module(Module *module) noexcept { for (auto &f : module->functions()) { functions.emplace_back(&f); } for (auto &f : module->functions()) { auto func_info = outline_pass_run_on_function(module, &f); - } return info; } -} +}// namespace luisa::compute::xir diff --git a/src/xir/passes/sink_alloca.cpp b/src/xir/passes/sink_alloca.cpp new file mode 100644 index 000000000..b6869ad8e --- /dev/null +++ b/src/xir/passes/sink_alloca.cpp @@ -0,0 +1,53 @@ +#include +#include +#include + +namespace luisa::compute::xir { + +class AllocaInstSinker { + +private: + static void _collect_alloca_insts_in_block(luisa::vector collected, BasicBlock *bb) noexcept { + } + +private: + [[nodiscard]] static bool _try_sink_alloca_inst(AllocaInst *inst) noexcept { + } + +public: + [[nodiscard]] static SinkAllocaInfo run(FunctionDefinition *f) noexcept { + luisa::vector collected; + _collect_alloca_insts_in_block(collected, f->body_block()); + collected.erase(std::remove_if(collected.begin(), collected.end(), [](AllocaInst *inst) noexcept { + return !_try_sink_alloca_inst(inst); + }), + collected.end()); + return {std::move(collected)}; + } +}; + +SinkAllocaInfo sink_alloca_pass_run_on_function(Module *module, Function *function) noexcept { + return function->is_definition() ? + AllocaInstSinker::run(static_cast(function)) : + SinkAllocaInfo{}; +} + +void merge_sink_alloca_info(SinkAllocaInfo &result, const SinkAllocaInfo &info) noexcept { + result.sunken_alloca_insts.reserve(result.sunken_alloca_insts.size() + info.sunken_alloca_insts.size()); + for (auto inst : info.sunken_alloca_insts) { + result.sunken_alloca_insts.emplace_back(inst); + } +} + +SinkAllocaInfo sink_alloca_pass_run_on_module(Module *module) noexcept { + SinkAllocaInfo info; + for (auto &f : module->functions()) { + if (f.is_definition()) { + auto func_info = sink_alloca_pass_run_on_function(module, &f); + merge_sink_alloca_info(info, func_info); + } + } + return info; +} + +}// namespace luisa::compute::xir