From 01032726327bf1f68224fd0f5dd9a6178efb2b74 Mon Sep 17 00:00:00 2001 From: Mike Smith Date: Fri, 10 Jan 2025 16:49:53 +0800 Subject: [PATCH] minor fix --- src/backends/fallback/fallback_codegen.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/backends/fallback/fallback_codegen.cpp b/src/backends/fallback/fallback_codegen.cpp index 2b05cf356..156884552 100644 --- a/src/backends/fallback/fallback_codegen.cpp +++ b/src/backends/fallback/fallback_codegen.cpp @@ -3203,6 +3203,24 @@ class FallbackCodegen { } // translate body static_cast(_translate_basic_block(current, f->body_block())); + // we should hoist all alloca instructions to the beginning of the function + { + luisa::vector alloca_insts; + for (auto &&llvm_bb : *llvm_func) { + for (auto &&llvm_inst : llvm_bb) { + if (auto alloca_inst = llvm::dyn_cast(&llvm_inst)) { + alloca_insts.emplace_back(alloca_inst); + } + } + } + // reverse the order of alloca instructions for better readability + std::reverse(alloca_insts.begin(), alloca_insts.end()); + // move alloca instructions to the beginning of the function + auto &llvm_entry = llvm_func->getEntryBlock(); + for (auto inst : alloca_insts) { + inst->moveBefore(&llvm_entry.front()); + } + } // return return llvm_func; }