CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
180 lines
5.4 KiB
C
180 lines
5.4 KiB
C
//===-------- BasicOrcV2CBindings.c - Basic OrcV2 C Bindings Demo ---------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "llvm-c/Core.h"
|
|
#include "llvm-c/Error.h"
|
|
#include "llvm-c/LLJIT.h"
|
|
#include "llvm-c/Support.h"
|
|
#include "llvm-c/Target.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
int handleError(LLVMErrorRef Err) {
|
|
char *ErrMsg = LLVMGetErrorMessage(Err);
|
|
fprintf(stderr, "Error: %s\n", ErrMsg);
|
|
LLVMDisposeErrorMessage(ErrMsg);
|
|
return 1;
|
|
}
|
|
|
|
LLVMOrcThreadSafeModuleRef createDemoModule(void) {
|
|
// Create an LLVMContext.
|
|
LLVMContextRef Ctx = LLVMContextCreate();
|
|
|
|
// Create a new LLVM module.
|
|
LLVMModuleRef M = LLVMModuleCreateWithNameInContext("demo", Ctx);
|
|
|
|
// Add a "sum" function":
|
|
// - Create the function type and function instance.
|
|
LLVMTypeRef Int32Type = LLVMInt32TypeInContext(Ctx);
|
|
LLVMTypeRef ParamTypes[] = {Int32Type, Int32Type};
|
|
LLVMTypeRef SumFunctionType = LLVMFunctionType(Int32Type, ParamTypes, 2, 0);
|
|
LLVMValueRef SumFunction = LLVMAddFunction(M, "sum", SumFunctionType);
|
|
|
|
// - Add a basic block to the function.
|
|
LLVMBasicBlockRef EntryBB =
|
|
LLVMAppendBasicBlockInContext(Ctx, SumFunction, "entry");
|
|
|
|
// - Add an IR builder and point it at the end of the basic block.
|
|
LLVMBuilderRef Builder = LLVMCreateBuilderInContext(Ctx);
|
|
LLVMPositionBuilderAtEnd(Builder, EntryBB);
|
|
|
|
// - Get the two function arguments and use them co construct an "add"
|
|
// instruction.
|
|
LLVMValueRef SumArg0 = LLVMGetParam(SumFunction, 0);
|
|
LLVMValueRef SumArg1 = LLVMGetParam(SumFunction, 1);
|
|
LLVMValueRef Result = LLVMBuildAdd(Builder, SumArg0, SumArg1, "result");
|
|
|
|
// - Build the return instruction.
|
|
LLVMBuildRet(Builder, Result);
|
|
|
|
// - Free the builder.
|
|
LLVMDisposeBuilder(Builder);
|
|
|
|
// Create a new ThreadSafeContext to hold the context.
|
|
LLVMOrcThreadSafeContextRef TSCtx =
|
|
LLVMOrcCreateNewThreadSafeContextFromLLVMContext(Ctx);
|
|
|
|
// Our demo module is now complete. Wrap it and our ThreadSafeContext in a
|
|
// ThreadSafeModule.
|
|
LLVMOrcThreadSafeModuleRef TSM = LLVMOrcCreateNewThreadSafeModule(M, TSCtx);
|
|
|
|
// Dispose of our local ThreadSafeContext value. The underlying LLVMContext
|
|
// will be kept alive by our ThreadSafeModule, TSM.
|
|
LLVMOrcDisposeThreadSafeContext(TSCtx);
|
|
|
|
// Return the result.
|
|
return TSM;
|
|
}
|
|
|
|
int main(int argc, const char *argv[]) {
|
|
|
|
int MainResult = 0;
|
|
|
|
// Parse command line arguments and initialize LLVM Core.
|
|
LLVMParseCommandLineOptions(argc, argv, "");
|
|
|
|
// Initialize native target codegen and asm printer.
|
|
LLVMInitializeNativeTarget();
|
|
LLVMInitializeNativeAsmPrinter();
|
|
|
|
// Create the JIT instance.
|
|
LLVMOrcLLJITRef J;
|
|
{
|
|
LLVMErrorRef Err;
|
|
if ((Err = LLVMOrcCreateLLJIT(&J, 0))) {
|
|
MainResult = handleError(Err);
|
|
goto llvm_shutdown;
|
|
}
|
|
}
|
|
|
|
// Create our demo module.
|
|
LLVMOrcThreadSafeModuleRef TSM = createDemoModule();
|
|
LLVMOrcResourceTrackerRef RT;
|
|
|
|
// Add our demo module to the JIT.
|
|
{
|
|
LLVMOrcJITDylibRef MainJD = LLVMOrcLLJITGetMainJITDylib(J);
|
|
RT = LLVMOrcJITDylibCreateResourceTracker(MainJD);
|
|
LLVMErrorRef Err;
|
|
if ((Err = LLVMOrcLLJITAddLLVMIRModuleWithRT(J, RT, TSM))) {
|
|
// If adding the ThreadSafeModule fails then we need to clean it up
|
|
// ourselves. If adding it succeeds the JIT will manage the memory.
|
|
LLVMOrcDisposeThreadSafeModule(TSM);
|
|
MainResult = handleError(Err);
|
|
goto jit_cleanup;
|
|
}
|
|
}
|
|
|
|
// Look up the address of our demo entry point.
|
|
printf("Looking up before removal...\n");
|
|
LLVMOrcJITTargetAddress SumAddr;
|
|
{
|
|
LLVMErrorRef Err;
|
|
if ((Err = LLVMOrcLLJITLookup(J, &SumAddr, "sum"))) {
|
|
MainResult = handleError(Err);
|
|
goto jit_cleanup;
|
|
}
|
|
}
|
|
|
|
// If we made it here then everything succeeded. Execute our JIT'd code.
|
|
int32_t (*Sum)(int32_t, int32_t) = (int32_t(*)(int32_t, int32_t))SumAddr;
|
|
int32_t Result = Sum(1, 2);
|
|
|
|
// Print the result.
|
|
printf("1 + 2 = %i\n", Result);
|
|
|
|
// Remove the code.
|
|
{
|
|
LLVMErrorRef Err;
|
|
if ((Err = LLVMOrcResourceTrackerRemove(RT))) {
|
|
MainResult = handleError(Err);
|
|
goto jit_cleanup;
|
|
}
|
|
}
|
|
|
|
// Attempt a second lookup. Here we expect an error as the code and symbols
|
|
// should have been removed.
|
|
printf("Attempting to remove code / symbols...\n");
|
|
{
|
|
LLVMOrcJITTargetAddress ThrowAwayAddress;
|
|
LLVMErrorRef Err = LLVMOrcLLJITLookup(J, &ThrowAwayAddress, "sum");
|
|
if (Err) {
|
|
printf("Received error as expected:\n");
|
|
handleError(Err);
|
|
} else {
|
|
printf("Failure: Second lookup should have generated an error.\n");
|
|
MainResult = 1;
|
|
}
|
|
}
|
|
|
|
jit_cleanup:
|
|
// Destroy our JIT instance. This will clean up any memory that the JIT has
|
|
// taken ownership of. This operation is non-trivial (e.g. it may need to
|
|
// JIT static destructors) and may also fail. In that case we want to render
|
|
// the error to stderr, but not overwrite any existing return value.
|
|
|
|
printf("Releasing resource tracker...\n");
|
|
LLVMOrcReleaseResourceTracker(RT);
|
|
|
|
printf("Destroying LLJIT instance and exiting.\n");
|
|
{
|
|
LLVMErrorRef Err;
|
|
if ((Err = LLVMOrcDisposeLLJIT(J))) {
|
|
int NewFailureResult = handleError(Err);
|
|
if (MainResult == 0)
|
|
MainResult = NewFailureResult;
|
|
}
|
|
}
|
|
|
|
llvm_shutdown:
|
|
// Shut down LLVM.
|
|
LLVMShutdown();
|
|
|
|
return MainResult;
|
|
}
|