--- mesa/src/gallium/frontends/clover/llvm/invocation.cpp 2023-01-21 08:54:07.903736459 -0400 +++ invocation.cpp 2023-09-04 14:06:25.213896411 -0300 @@ -27,13 +27,17 @@ #include #include #include +#include #include -#include +#include #include #ifdef HAVE_CLOVER_SPIRV #include #endif +#include +#include +#include #include #include #include @@ -439,10 +443,10 @@ clover::llvm::compile_program(const std: namespace { void - optimize(Module &mod, unsigned optimization_level, + optimize(Module &mod, + const std::string& ir_target, + unsigned optimization_level, bool internalize_symbols) { - ::llvm::legacy::PassManager pm; - // By default, the function internalizer pass will look for a function // called "main" and then mark all other functions as internal. Marking // functions as internal enables the optimizer to perform optimizations @@ -458,19 +462,53 @@ namespace { if (internalize_symbols) { std::vector names = map(std::mem_fn(&Function::getName), get_kernels(mod)); - pm.add(::llvm::createInternalizePass( + internalizeModule(mod, [=](const ::llvm::GlobalValue &gv) { return std::find(names.begin(), names.end(), gv.getName()) != names.end(); - })); + }); + } + + + const char *opt_str = NULL; + LLVMCodeGenOptLevel level; + switch (optimization_level) { + case 0: + default: + opt_str = "default"; + level = LLVMCodeGenLevelNone; + break; + case 1: + opt_str = "default"; + level = LLVMCodeGenLevelLess; + break; + case 2: + opt_str = "default"; + level = LLVMCodeGenLevelDefault; + break; + case 3: + opt_str = "default"; + level = LLVMCodeGenLevelAggressive; + break; } - ::llvm::PassManagerBuilder pmb; - pmb.OptLevel = optimization_level; - pmb.LibraryInfo = new ::llvm::TargetLibraryInfoImpl( - ::llvm::Triple(mod.getTargetTriple())); - pmb.populateModulePassManager(pm); - pm.run(mod); + const target &target = ir_target; + LLVMTargetRef targ; + char *err_message; + + if (LLVMGetTargetFromTriple(target.triple.c_str(), &targ, &err_message)) + return; + LLVMTargetMachineRef tm = + LLVMCreateTargetMachine(targ, target.triple.c_str(), + target.cpu.c_str(), "", level, + LLVMRelocDefault, LLVMCodeModelDefault); + + if (!tm) + return; + LLVMPassBuilderOptionsRef opts = LLVMCreatePassBuilderOptions(); + LLVMRunPasses(wrap(&mod), opt_str, tm, opts); + + LLVMDisposeTargetMachine(tm); } std::unique_ptr @@ -500,7 +538,7 @@ clover::llvm::link_program(const std::ve auto c = create_compiler_instance(dev, dev.ir_target(), options, r_log); auto mod = link(*ctx, *c, binaries, r_log); - optimize(*mod, c->getCodeGenOpts().OptimizationLevel, !create_library); + optimize(*mod, dev.ir_target(), c->getCodeGenOpts().OptimizationLevel, !create_library); static std::atomic_uint seq(0); const std::string id = "." + mod->getModuleIdentifier() + "-" +