Skip to content

Commit

Permalink
new passmanager, lto support (#53)
Browse files Browse the repository at this point in the history
* regenerate llvm with new `c2nim`
* propagate cpu selection / features to llvm ir (aka "-mcpu=native")
* pass `--passc` flags to llvm as well, for increased clang compatibilty
  • Loading branch information
arnetheduck committed Sep 13, 2023
1 parent 32b4613 commit 1fb9bca
Show file tree
Hide file tree
Showing 24 changed files with 1,971 additions and 1,747 deletions.
67 changes: 6 additions & 61 deletions llvm/llvm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ type
opaqueModuleFlagEntry{.pure, final.} = object
OpaqueBinary{.pure, final.} = object
OpaqueError{.pure, final.} = object
OpaquePassBuilderOptions{.pure, final.} = object


## Orc.nim
OrcOpaqueExecutionSession{.pure, final.} = object
Expand Down Expand Up @@ -105,16 +107,6 @@ type
OrcOpaqueLLJITBuilder{.pure, final.} = object
OrcOpaqueLLJIT{.pure, final.} = object

OrcJITTargetAddress* = uint64

# Funny type names that came out of c2nim
int64T = int64
uint64T = uint64
uint8T = uint8

Bool* = cint
AttributeIndex* = cuint

OpaqueTargetData{.pure, final.} = object
OpaqueTargetLibraryInfotData{.pure, final.} = object

Expand All @@ -134,48 +126,9 @@ type
CallBr = 67, ## Standard Unary Operators
Freeze = 68 ## Atomic operators

DIFlags* {.size: sizeof(cint).} = enum
DIFlagZero = 0, DIFlagPrivate = 1, DIFlagProtected = 2, DIFlagPublic = 3,
DIFlagFwdDecl = 1 shl 2, DIFlagAppleBlock = 1 shl 3, DIFlagReservedBit4 = 1 shl 4,
DIFlagVirtual = 1 shl 5, DIFlagArtificial = 1 shl 6, DIFlagExplicit = 1 shl 7,
DIFlagPrototyped = 1 shl 8, DIFlagObjcClassComplete = 1 shl 9,
DIFlagObjectPointer = 1 shl 10, DIFlagVector = 1 shl 11,
DIFlagStaticMember = 1 shl 12, DIFlagLValueReference = 1 shl 13,
DIFlagRValueReference = 1 shl 14, DIFlagReserved = 1 shl 15,
DIFlagSingleInheritance = 1 shl 16, DIFlagMultipleInheritance = 2 shl 16,
DIFlagVirtualInheritance = 3 shl 16, DIFlagIntroducedVirtual = 1 shl 18,
DIFlagBitField = 1 shl 19, DIFlagNoReturn = 1 shl 20,
DIFlagTypePassByValue = 1 shl 22, DIFlagTypePassByReference = 1 shl 23,
DIFlagEnumClass = 1 shl 24,
DIFlagThunk = 1 shl 25, DIFlagNonTrivial = 1 shl 26, DIFlagBigEndian = 1 shl 27,
DIFlagLittleEndian = 1 shl 28,
#DIFlagIndirectVirtualBase = (1 shl 2) or (1 shl 5),
#DIFlagAccessibility = dIFlagPrivate or dIFlagProtected or dIFlagPublic, DIFlagPtrToMemberRep = dIFlagSingleInheritance or
# dIFlagMultipleInheritance or dIFlagVirtualInheritance

DWARFTypeEncoding* = cuint
MetadataKind* = cuint

ByteOrdering* {.size: sizeof(cint).} = enum
BigEndian, LittleEndian

TargetMachineRef* = ptr OpaqueTargetMachine
PassManagerBuilderRef* = ptr OpaquePassManagerBuilder

ComdatSelectionKind* {.size: sizeof(cint).} = enum
AnyComdatSelectionKind, ## The linker may choose any COMDAT.
ExactMatchComdatSelectionKind, ## The data referenced by the COMDAT must
## be the same.
LargestComdatSelectionKind, ## The linker will choose the largest
## COMDAT.
NoDeduplicateComdatSelectionKind, ## No deduplication is performed.
SameSizeComdatSelectionKind ## The data referenced by the COMDAT must be
## the same size.

ErrorRef* = ptr OpaqueError

MemoryManagerCreateContextCallback* = proc(ctxCtx: pointer): pointer {.cdecl, raises: [].}

include llvm/Types
include llvm/Support

Expand All @@ -189,14 +142,12 @@ include llvm/IRReader
include llvm/Linker
include llvm/Target
include llvm/TargetMachine
include llvm/Transforms/PassBuilder
include llvm/Transforms/PassManagerBuilder

include llvm/ExecutionEngine
include llvm/Orc
include llvm/OrcEE

type OrcLLJITBuilderObjectLinkingLayerCreatorFunction* = proc(ctx: pointer, ES: OrcExecutionSessionRef, triple: cstring): OrcObjectLayerRef {.cdecl, raises: [].}

include llvm/LLJIT

include preprocessed
Expand All @@ -222,18 +173,11 @@ const
DW_ATE_lo_user* = 0x80.cuint
DW_ATE_hi_user* = 0xff.cuint

proc parseIRInContext*(contextRef: ContextRef; memBuf: MemoryBufferRef;
outM: ptr ModuleRef; outMessage: cstringArray): Bool {.
importc: "LLVMParseIRInContext", dynlib: LLVMLib.}

proc getModuleDataLayout*(m: ModuleRef): TargetDataRef {.
importc: "LLVMGetModuleDataLayout", dynlib: LLVMLib.}

proc typeOfX*(val: ValueRef): TypeRef {.importc: "LLVMTypeOf", dynlib: LLVMLib.}

proc passManagerBuilderCreate*(): PassManagerBuilderRef {.
importc: "LLVMPassManagerBuilderCreate", dynlib: LLVMLib.}

template oaAddr(v: openArray): untyped =
if v.len > 0: v[0].unsafeAddr else: nil
template oaLen(v: openArray): cuint = v.len.uint32
Expand Down Expand Up @@ -332,11 +276,12 @@ proc nimLLDLinkWasm*(args: openArray[string]): bool =
nimLLDLinkWasm(argv, args.len.csize_t)


proc nimCreateTargetMachine*(t: TargetRef; triple: cstring; cpu: cstring;
features: cstring; level: CodeGenOptLevel;
proc nimCreateTargetMachine*(t: TargetRef; triple: cstring; level: CodeGenOptLevel;
reloc: RelocMode; codeModel: CodeModel): TargetMachineRef {.
importc: "LLVMNimCreateTargetMachine".}

proc nimSetFunctionAttributes*(F: ValueRef) {.
importc: "LLVMNimSetFunctionAttributes".}

# A few helpers to make things more smooth

Expand Down
32 changes: 16 additions & 16 deletions llvm/llvm/BitReader.nim
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,21 @@
## |* *|
## \*===----------------------------------------------------------------------===

## !!!Ignored construct: # LLVM_C_BITREADER_H [NewLine] # LLVM_C_BITREADER_H [NewLine] # llvm-c/ExternC.h [NewLine] # llvm-c/Types.h [NewLine] LLVM_C_EXTERN_C_BEGIN *
##
## @defgroup LLVMCBitReader Bit Reader
## @ingroup LLVMC
##
## @{
## Builds a module from the bitcode in the specified memory buffer, returning a
##
## Builds a module from the bitcode in the specified memory buffer, returning a
## reference to the module via the OutModule parameter. Returns 0 on success.
## Optionally returns a human-readable error message via OutMessage.
##
## This is deprecated. Use LLVMParseBitcode2. LLVMBool LLVMParseBitcode ( LLVMMemoryBufferRef MemBuf , LLVMModuleRef * OutModule , char * * OutMessage ) ;
## Error: expected ';'!!!
## This is deprecated. Use LLVMParseBitcode2.

proc parseBitcode*(memBuf: MemoryBufferRef; outModule: ptr ModuleRef;
outMessage: cstringArray): Bool {.
importc: "LLVMParseBitcode", dynlib: LLVMLib.}
## Builds a module from the bitcode in the specified memory buffer, returning a
## reference to the module via the OutModule parameter. Returns 0 on success.

Expand All @@ -36,40 +39,37 @@ proc parseBitcode2*(memBuf: MemoryBufferRef; outModule: ptr ModuleRef): Bool {.
## This is deprecated. Use LLVMParseBitcodeInContext2.

proc parseBitcodeInContext*(contextRef: ContextRef; memBuf: MemoryBufferRef;
outModule: ptr ModuleRef; outMessage: cstringArray): Bool {.
outModule: ptr ModuleRef; outMessage: cstringArray): Bool {.
importc: "LLVMParseBitcodeInContext", dynlib: LLVMLib.}
proc parseBitcodeInContext2*(contextRef: ContextRef; memBuf: MemoryBufferRef;
outModule: ptr ModuleRef): Bool {.
outModule: ptr ModuleRef): Bool {.
importc: "LLVMParseBitcodeInContext2", dynlib: LLVMLib.}
## * Reads a module from the specified path, returning via the OutMP parameter
## Reads a module from the specified path, returning via the OutMP parameter
## a module provider which performs lazy deserialization. Returns 0 on success.
## Optionally returns a human-readable error message via OutMessage.
## This is deprecated. Use LLVMGetBitcodeModuleInContext2.

proc getBitcodeModuleInContext*(contextRef: ContextRef; memBuf: MemoryBufferRef;
outM: ptr ModuleRef; outMessage: cstringArray): Bool {.
outM: ptr ModuleRef; outMessage: cstringArray): Bool {.
importc: "LLVMGetBitcodeModuleInContext", dynlib: LLVMLib.}
## * Reads a module from the given memory buffer, returning via the OutMP
## Reads a module from the given memory buffer, returning via the OutMP
## parameter a module provider which performs lazy deserialization.
##
## Returns 0 on success.
##
## Takes ownership of \p MemBuf if (and only if) the module was read
## successfully.

proc getBitcodeModuleInContext2*(contextRef: ContextRef; memBuf: MemoryBufferRef;
outM: ptr ModuleRef): Bool {.
proc getBitcodeModuleInContext2*(contextRef: ContextRef;
memBuf: MemoryBufferRef; outM: ptr ModuleRef): Bool {.
importc: "LLVMGetBitcodeModuleInContext2", dynlib: LLVMLib.}
## This is deprecated. Use LLVMGetBitcodeModule2.

proc getBitcodeModule*(memBuf: MemoryBufferRef; outM: ptr ModuleRef;
outMessage: cstringArray): Bool {.
outMessage: cstringArray): Bool {.
importc: "LLVMGetBitcodeModule", dynlib: LLVMLib.}
proc getBitcodeModule2*(memBuf: MemoryBufferRef; outM: ptr ModuleRef): Bool {.
importc: "LLVMGetBitcodeModule2", dynlib: LLVMLib.}
## *
##
## @}
##

## !!!Ignored construct: LLVM_C_EXTERN_C_END # [NewLine]
## Error: expected ';'!!!
23 changes: 12 additions & 11 deletions llvm/llvm/BitWriter.nim
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,31 @@
## |* *|
## \*===----------------------------------------------------------------------===

## !!!Ignored construct: # LLVM_C_BITWRITER_H [NewLine] # LLVM_C_BITWRITER_H [NewLine] # llvm-c/ExternC.h [NewLine] # llvm-c/Types.h [NewLine] LLVM_C_EXTERN_C_BEGIN *
##
## @defgroup LLVMCBitWriter Bit Writer
## @ingroup LLVMC
##
## @{
## ===-- Operations on modules ---------------------------------------------=== * Writes a module to the specified path. Returns 0 on success. int LLVMWriteBitcodeToFile ( LLVMModuleRef M , const char * Path ) ;
## Error: expected ';'!!!
##
## ===-- Operations on modules ---------------------------------------------===
## Writes a module to the specified path. Returns 0 on success.

## * Writes a module to an open file descriptor. Returns 0 on success.
proc writeBitcodeToFile*(m: ModuleRef; path: cstring): cint {.
importc: "LLVMWriteBitcodeToFile", dynlib: LLVMLib.}
## Writes a module to an open file descriptor. Returns 0 on success.

proc writeBitcodeToFD*(m: ModuleRef; fd: cint; shouldClose: cint; unbuffered: cint): cint {.
proc writeBitcodeToFD*(m: ModuleRef; fd: cint; shouldClose: cint;
unbuffered: cint): cint {.
importc: "LLVMWriteBitcodeToFD", dynlib: LLVMLib.}
## * Deprecated for LLVMWriteBitcodeToFD. Writes a module to an open file
## Deprecated for LLVMWriteBitcodeToFD. Writes a module to an open file
## descriptor. Returns 0 on success. Closes the Handle.

proc writeBitcodeToFileHandle*(m: ModuleRef; handle: cint): cint {.
importc: "LLVMWriteBitcodeToFileHandle", dynlib: LLVMLib.}
## * Writes a module to a new memory buffer and returns it.
## Writes a module to a new memory buffer and returns it.

proc writeBitcodeToMemoryBuffer*(m: ModuleRef): MemoryBufferRef {.
importc: "LLVMWriteBitcodeToMemoryBuffer", dynlib: LLVMLib.}
## *
##
## @}
##

## !!!Ignored construct: LLVM_C_EXTERN_C_END # [NewLine]
## Error: expected ';'!!!
37 changes: 22 additions & 15 deletions llvm/llvm/Comdat.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,26 @@
## |* *|
## \*===----------------------------------------------------------------------===

## !!!Ignored construct: # LLVM_C_COMDAT_H [NewLine] # LLVM_C_COMDAT_H [NewLine] # llvm-c/ExternC.h [NewLine] # llvm-c/Types.h [NewLine] LLVM_C_EXTERN_C_BEGIN *
##
## @defgroup LLVMCCoreComdat Comdats
## @ingroup LLVMCCore
##
## @{
## typedef enum { LLVMAnyComdatSelectionKind , /< The linker may choose any COMDAT. LLVMExactMatchComdatSelectionKind , /< The data referenced by the COMDAT must
## /< be the same. LLVMLargestComdatSelectionKind , /< The linker will choose the largest
## /< COMDAT. LLVMNoDeduplicateComdatSelectionKind , /< No deduplication is performed. LLVMSameSizeComdatSelectionKind /< The data referenced by the COMDAT must be
## /< the same size. } LLVMComdatSelectionKind ;
## Error: expected ';'!!!
##

type
ComdatSelectionKind* {.size: sizeof(cint).} = enum
AnyComdatSelectionKind, ## < The linker may choose any COMDAT.
ExactMatchComdatSelectionKind, ## < The data referenced by the COMDAT must
## < be the same.
LargestComdatSelectionKind, ## < The linker will choose the largest
## < COMDAT.
NoDeduplicateComdatSelectionKind, ## < No deduplication is performed.
SameSizeComdatSelectionKind ## < The data referenced by the COMDAT must be
## < the same size.

## *

##
## Return the Comdat in the module with the specified name. It is created
## if it didn't already exist.
##
Expand All @@ -31,20 +39,22 @@
proc getOrInsertComdat*(m: ModuleRef; name: cstring): ComdatRef {.
importc: "LLVMGetOrInsertComdat", dynlib: LLVMLib.}
## *
##
## Get the Comdat assigned to the given global object.
##
## @see llvm::GlobalObject::getComdat()
##
proc getComdat*(v: ValueRef): ComdatRef {.importc: "LLVMGetComdat", dynlib: LLVMLib.}
## *
proc getComdat*(v: ValueRef): ComdatRef {.importc: "LLVMGetComdat",
dynlib: LLVMLib.}
##
## Assign the Comdat to the given global object.
##
## @see llvm::GlobalObject::setComdat()
##
proc setComdat*(v: ValueRef; c: ComdatRef) {.importc: "LLVMSetComdat", dynlib: LLVMLib.}
proc setComdat*(v: ValueRef; c: ComdatRef) {.importc: "LLVMSetComdat",
dynlib: LLVMLib.}
##
## Get the conflict resolution selection kind for the Comdat.
##
Expand All @@ -61,9 +71,6 @@ proc getComdatSelectionKind*(c: ComdatRef): ComdatSelectionKind {.
proc setComdatSelectionKind*(c: ComdatRef; kind: ComdatSelectionKind) {.
importc: "LLVMSetComdatSelectionKind", dynlib: LLVMLib.}
## *
##
## @}
##
## !!!Ignored construct: LLVM_C_EXTERN_C_END # [NewLine]
## Error: expected ';'!!!
Loading

0 comments on commit 1fb9bca

Please sign in to comment.