Skip to content

Commit

Permalink
upd(llvm): replace old PassManager by the new PassBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
mratsim committed Jul 21, 2024
1 parent 8685e7c commit 36ac6e3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 68 deletions.
38 changes: 14 additions & 24 deletions constantine/math_compiler/codegen_nvidia.nim
Original file line number Diff line number Diff line change
Expand Up @@ -187,30 +187,20 @@ proc codegenNvidiaPTX*(asy: Assembler_LLVM, sm: tuple[major, minor: int32]): str
codeModel = CodeModelDefault
)

# https://www.llvm.org/docs/Passes.html
let pm = createPassManager()

machine.addAnalysisPasses(pm)
pm.addDeduceFunctionAttributesPass()
pm.addMemCpyOptPass()
pm.addScalarReplacementOfAggregatesPass()
pm.addPromoteMemoryToRegisterPass()
pm.addGlobalValueNumberingPass()
pm.addDeadStoreEliminationPass()
pm.addInstructionCombiningPass()
pm.addFunctionInliningPass()
pm.addAggressiveDeadCodeEliminationPass()

when false:
# As most (all?) of our code is straightline, unoptimizable inline assembly, no loop and no branches
# most optimizations, even at -O3, are not applicable
let pmb = createPassManagerBuilder()
pmb.setOptLevel(3)
pmb.populateModulePassManager(pm)
pmb.dispose()

pm.run(asy.module)
pm.dispose()
let pbo = createPassBuilderOptions()
pbo.setMergeFunctions()
let err = asy.module.runPasses(
"default<O3>,function-attrs,memcpyopt,sroa,mem2reg,gvn,dse,instcombine,inline,adce",
machine,
pbo
)
if not err.pointer().isNil():
writeStackTrace()
let errMsg = err.getErrorMessage()
stderr.write("\"codegenNvidiaPTX\" for module '" & astToStr(module) & "' " & $instantiationInfo() &
" exited with error: " & $cstring(errMsg) & '\n')
errMsg.dispose()
quit 1

return machine.emitToString(asy.module, AssemblyFile)

Expand Down
46 changes: 2 additions & 44 deletions constantine/platforms/llvm/bindings/llvm_abi.nim
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ type
TargetRef* = distinct pointer
ExecutionEngineRef* = distinct pointer
TargetMachineRef* = distinct pointer
PassManagerRef* = distinct pointer
PassManagerBuilderRef* = distinct pointer
PassBuilderOptionsRef* = distinct pointer
TypeRef* = distinct pointer
ValueRef* = distinct pointer
Expand Down Expand Up @@ -215,53 +213,13 @@ proc targetMachineEmitToMemoryBuffer*(t: TargetMachineRef, m: ModuleRef,
# - https://blog.llvm.org/posts/2021-03-26-the-new-pass-manager/
# - https://llvm.org/docs/NewPassManager.html

# https://llvm.org/doxygen/group__LLVMCCorePassManagers.html
# # header: "<llvm-c/Core.h>"

proc createPassManager*(): PassManagerRef {.importc: "LLVMCreatePassManager".}
proc dispose*(pm: PassManagerRef) {.importc: "LLVMDisposePassManager".}
proc run*(pm: PassManagerRef, module: ModuleRef) {. importc: "LLVMRunPassManager".}

# https://llvm.org/doxygen/group__LLVMCTransformsPassManagerBuilder.html
# header: "<llvm-c/Transforms/PassManagerBuilder.h>"

proc createPassManagerBuilder*(): PassManagerBuilderRef {.importc: "LLVMPassManagerBuilderCreate".}
proc dispose*(pmb: PassManagerBuilderRef) {.importc: "LLVMPassManagerBuilderDispose".}
proc setOptLevel*(pmb: PassManagerBuilderRef, level: uint32) {.importc: "LLVMPassManagerBuilderSetOptLevel".}
proc setSizeLevel*(pmb: PassManagerBuilderRef, level: uint32) {.importc: "LLVMPassManagerBuilderSetSizeLevel".}
proc populateModulePassManager*(pmb: PassManagerBuilderRef, legacyPM: PassManagerRef) {. importc: "LLVMPassManagerBuilderPopulateModulePassManager".}

# https://llvm.org/doxygen/group__LLVMCCoreNewPM.html
# header: "<llvm-c/Transforms/PassBuilder.h>"

proc createPassBuilderOptions*(): PassBuilderOptionsRef {.importc: "LLVMCreatePassBuilderOptions".}
proc dispose*(pbo: PassBuilderOptionsRef) {.importc: "LLVMDisposePassBuilderOptions".}
proc runPasses(module: ModuleRef, passes: cstring, machine: TargetMachineRef, pbo: PassBuilderOptionsRef): ErrorRef {.used, importc: "LLVMRunPasses".}

# https://llvm.org/doxygen/group__LLVMCTarget.html
proc addTargetLibraryInfo*(tli: TargetLibraryInfoRef, pm: PassManagerRef) {.importc: "LLVMAddTargetLibraryInfo".}
# There doesn't seem to be a way to instantiate TargetLibraryInfoRef :/
proc addAnalysisPasses*(machine: TargetMachineRef, pm: PassManagerRef) {.importc: "LLVMAddAnalysisPasses".}

# https://www.llvm.org/docs/Passes.html
# -------------------------------------

# https://llvm.org/doxygen/group__LLVMCTransformsInstCombine.html
proc addInstructionCombiningPass*(pm: PassManagerRef) {.importc: "LLVMAddInstructionCombiningPass".}

# https://llvm.org/doxygen/group__LLVMCTransformsUtils.html
proc addPromoteMemoryToRegisterPass*(pm: PassManagerRef) {.importc: "LLVMAddPromoteMemoryToRegisterPass".}

# https://llvm.org/doxygen/group__LLVMCTransformsScalar.html
proc addAggressiveDeadCodeEliminationPass*(pm: PassManagerRef) {.importc: "LLVMAddAggressiveDCEPass".}
proc addDeadStoreEliminationPass*(pm: PassManagerRef) {.importc: "LLVMAddDeadStoreEliminationPass".}
proc addGlobalValueNumberingPass*(pm: PassManagerRef) {.importc: "LLVMAddNewGVNPass".}
proc addMemCpyOptPass*(pm: PassManagerRef) {.importc: "LLVMAddMemCpyOptPass".}
proc addScalarReplacementOfAggregatesPass*(pm: PassManagerRef) {.importc: "LLVMAddScalarReplAggregatesPass".}

# https://llvm.org/doxygen/group__LLVMCTransformsIPO.html
proc addDeduceFunctionAttributesPass*(pm: PassManagerRef) {.importc: "LLVMAddFunctionAttrsPass".}
proc addFunctionInliningPass*(pm: PassManagerRef) {.importc: "LLVMAddFunctionInliningPass".}
proc runPasses*(module: ModuleRef, passes: cstring, machine: TargetMachineRef, pbo: PassBuilderOptionsRef): ErrorRef {.used, importc: "LLVMRunPasses".}
proc setMergeFunctions*(pbo: PassBuilderOptionsRef, mergeFunctions = LlvmBool(1)) {.importc: "LLVMPassBuilderOptionsSetMergeFunctions".}

# ############################################################
#
Expand Down

0 comments on commit 36ac6e3

Please sign in to comment.