Skip to content

Add native libLLVMSharp helpers for C++ API surface absent from the C-API#271

Merged
tannergooding merged 11 commits into
dotnet:mainfrom
tannergooding:tannergooding-native-libllvmsharp-helpers
Jul 16, 2026
Merged

Add native libLLVMSharp helpers for C++ API surface absent from the C-API#271
tannergooding merged 11 commits into
dotnet:mainfrom
tannergooding:tannergooding-native-libllvmsharp-helpers

Conversation

@tannergooding

Copy link
Copy Markdown
Member

Expand the native libLLVMSharp C++ shim with helpers that expose llvm:: C++ API surface which the LLVM-C API does not, so the managed high-level API can faithfully mirror the LLVM C++ class hierarchy. Authored against the LLVM 21.1.8 C++ API, matching the existing unwrap/wrap + out_size/out_buffer conventions in the file.

Note

Requires a native rebuild + interop regen before the managed consumers can bind to these. These add native symbols only; the generated interop (sources/LLVMSharp.Interop/llvmsharp/) is regenerated from the rebuilt libLLVMSharp. There are no managed consumers in this PR — those follow after the libLLVMSharp.runtime.* packages are rebuilt.


IR helpersCmpInst.getPredicateName; Instruction opcode-name / isCommutative / mayHaveSideEffects / mayReadFromMemory / mayWriteToMemory; Type.getPrimitiveSizeInBits (scalable out-param) / getScalarSizeInBits; constant-folding & instruction-simplification; dominator-tree & loop-info analysis; TargetTransformInfo cost-model; function cloning & inlining; Value use/pointer-strip utilities (stripPointerCasts, getNumUses, isUsedInBasicBlock, ...); IR predicate & speculation-safety; basic-block insertion-point; loop induction-variable & exiting-block.

JITLink (resolves #263) — llvmsharp_OrcCreateObjectLinkingLayer, the JITLink ObjectLinkingLayer creator that the LLVM-C ORC API doesnt expose (the only real path off RtDyld-on-COFF). Links LLVMOrcJIT+LLVMJITLink`.

Cleanup — remove 6 redundant CmpInst instance predicate helpers now emulated managed-side from the predicate enum (#270).


Cannot be built in-session (no full LLVM install/headers here); helpers are authored against the LLVM 21.1.8 C++ signatures. The one spot to confirm on the native rebuild is the locally-redefined DEFINE_SIMPLE_CONVERSION_FUNCTIONS for orc::ExecutionSession/orc::ObjectLayer (distinct param types from the IR overloads, so no ambiguity expected).

tannergooding and others added 11 commits July 15, 2026 22:19
…-API

Add CallBase, CmpInst, GlobalValue, Instruction, Type, and Value helpers that
mirror LLVM C++ methods the LLVM-C API does not expose, and remove the now-unused
Instruction hasNoSignedWrap/hasNoUnsignedWrap helpers superseded by LLVMGetNSW/GetNUW.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Add llvmsharp_OrcCreateObjectLinkingLayer, returning an LLVMOrcObjectLayerRef
backed by llvm::orc::ObjectLinkingLayer for use with
LLVMOrcLLJITBuilderSetObjectLinkingLayerCreator, so the JIT can run object
linking on JITLink instead of RuntimeDyld. Link LLVMOrcJIT and LLVMJITLink to
resolve the ObjectLinkingLayer C++ symbols.

Resolves dotnet#263

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Expose ConstantFoldInstruction, ConstantFoldConstant, ConstantFoldCompareInstOperands, and simplifyInstruction from LLVMAnalysis, which the LLVM-C API does not surface. The opcode-based ConstantFoldBinaryOpOperands/ConstantFoldCastOperand folders are deferred since the internal Instruction opcode numbering does not match the C LLVMOpcode enum.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Expose DominatorTree, PostDominatorTree, LoopInfo, and Loop from LLVMAnalysis behind opaque handles with explicit create/dispose lifetimes, none of which the LLVM-C API surfaces. Loop handles returned by LoopInfo are non-owning and valid for the lifetime of the owning LoopInfo.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Expose a TargetTransformInfo derived from a TargetMachine and Function behind an opaque handle, along with getInstructionCost, getNumberOfRegisters, and getRegisterBitWidth, which the LLVM-C API does not surface. Requires a local DEFINE_SIMPLE_CONVERSION_FUNCTIONS(TargetMachine, ...) since LLVM keeps its own in the non-public TargetMachineC.cpp.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Expose CloneFunction and InlineFunction from LLVMTransformUtils, which the LLVM-C API does not surface. InlineFunction returns whether the inline succeeded.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Expose Mangler::getNameWithPrefix, Value::stripPointerCastsAndAliases, Value::stripInBoundsOffsets, Value::getPointerAlignment, Instruction::comesBefore/moveBefore/moveAfter, and GEPOperator::accumulateConstantOffset, none of which the LLVM-C API surfaces. getNameWithPrefix heap-allocates the mangled name; the caller frees it via llvmsharp_Free. moveBefore uses the iterator overload since the Instruction* overload is deprecated.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
The inverse/swapped predicate transforms and the signed/unsigned/true-when-equal/false-when-equal predicate classifications are pure functions of the predicate enum, so they are emulated managed-side from CmpInst.GetPredicate() and don't need a native round-trip.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@tannergooding
tannergooding marked this pull request as ready for review July 16, 2026 05:45
@tannergooding
tannergooding merged commit 978ce28 into dotnet:main Jul 16, 2026
12 checks passed
@tannergooding
tannergooding deleted the tannergooding-native-libllvmsharp-helpers branch July 16, 2026 05:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Expose a JITLink object-linking-layer creator via libLLVMSharp

1 participant