Skip to content

Commit

Permalink
Hide API removed in LLVM 17
Browse files Browse the repository at this point in the history
  • Loading branch information
QuLogic committed Sep 4, 2023
1 parent 8e7ec80 commit 4428c1b
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 7 deletions.
1 change: 0 additions & 1 deletion backports.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

#include "llvm-c/DebugInfo.h"
#include "llvm-c/Types.h"
#include "llvm-c/Transforms/PassManagerBuilder.h"

#ifdef __cplusplus
extern "C" {
Expand Down
94 changes: 94 additions & 0 deletions executionengine_llvm17_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
//go:build llvm17
// +build llvm17

//===- executionengine_test.go - Tests for executionengine ----------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file tests bindings for the executionengine component.
//
//===----------------------------------------------------------------------===//

package llvm

import (
"testing"
)

func TestFactorial(t *testing.T) {
LinkInMCJIT()
InitializeNativeTarget()
InitializeNativeAsmPrinter()

mod := NewModule("fac_module")

fac_args := []Type{Int32Type()}
fac_type := FunctionType(Int32Type(), fac_args, false)
fac := AddFunction(mod, "fac", fac_type)
fac.SetFunctionCallConv(CCallConv)
n := fac.Param(0)

entry := AddBasicBlock(fac, "entry")
iftrue := AddBasicBlock(fac, "iftrue")
iffalse := AddBasicBlock(fac, "iffalse")
end := AddBasicBlock(fac, "end")

builder := NewBuilder()
defer builder.Dispose()

builder.SetInsertPointAtEnd(entry)
If := builder.CreateICmp(IntEQ, n, ConstInt(Int32Type(), 0, false), "cmptmp")
builder.CreateCondBr(If, iftrue, iffalse)

builder.SetInsertPointAtEnd(iftrue)
res_iftrue := ConstInt(Int32Type(), 1, false)
builder.CreateBr(end)

builder.SetInsertPointAtEnd(iffalse)
n_minus := builder.CreateSub(n, ConstInt(Int32Type(), 1, false), "subtmp")
call_fac_args := []Value{n_minus}
call_fac := builder.CreateCall(fac_type, fac, call_fac_args, "calltmp")
res_iffalse := builder.CreateMul(n, call_fac, "multmp")
builder.CreateBr(end)

builder.SetInsertPointAtEnd(end)
res := builder.CreatePHI(Int32Type(), "result")
phi_vals := []Value{res_iftrue, res_iffalse}
phi_blocks := []BasicBlock{iftrue, iffalse}
res.AddIncoming(phi_vals, phi_blocks)
builder.CreateRet(res)

err := VerifyModule(mod, ReturnStatusAction)
if err != nil {
t.Errorf("Error verifying module: %s", err)
return
}

options := NewMCJITCompilerOptions()
options.SetMCJITOptimizationLevel(2)
options.SetMCJITEnableFastISel(true)
options.SetMCJITNoFramePointerElim(true)
options.SetMCJITCodeModel(CodeModelJITDefault)
engine, err := NewMCJITCompiler(mod, options)
if err != nil {
t.Errorf("Error creating JIT: %s", err)
return
}
defer engine.Dispose()

pass := NewPassManager()
defer pass.Dispose()

pass.Run(mod)

exec_args := []GenericValue{NewGenericValueFromInt(Int32Type(), 10, false)}
exec_res := engine.RunFunction(fac, exec_args)
var fac10 uint64 = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1
if exec_res.Int(false) != fac10 {
t.Errorf("Expected %d, got %d", fac10, exec_res.Int(false))
}
}
3 changes: 3 additions & 0 deletions executionengine_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build !llvm17
// +build !llvm17

//===- executionengine_test.go - Tests for executionengine ----------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
Expand Down
10 changes: 4 additions & 6 deletions ir.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ package llvm
#include <stdlib.h>
*/
import "C"
import "unsafe"
import "errors"
import (
"errors"
"unsafe"
)

type (
// We use these weird structs here because *Ref types are pointers and
Expand Down Expand Up @@ -985,10 +987,6 @@ func ConstIntCast(v Value, t Type, signed bool) (rv Value) {
return
}
func ConstFPCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstFPCast(v.C, t.C); return }
func ConstSelect(cond, iftrue, iffalse Value) (rv Value) {
rv.C = C.LLVMConstSelect(cond.C, iftrue.C, iffalse.C)
return
}
func ConstExtractElement(vec, i Value) (rv Value) {
rv.C = C.LLVMConstExtractElement(vec.C, i.C)
return
Expand Down
33 changes: 33 additions & 0 deletions ir_pre_llvm17.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//go:build !llvm17
// +build !llvm17

//===- ir.go - Bindings for ir --------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file defines bindings for the ir component.
//
//===----------------------------------------------------------------------===//

package llvm

/*
#include "llvm-c/Core.h"
#include "llvm-c/Comdat.h"
#include "IRBindings.h"
#include <stdlib.h>
*/
import "C"

//-------------------------------------------------------------------------
// llvm.Value
//-------------------------------------------------------------------------

func ConstSelect(cond, iftrue, iffalse Value) (rv Value) {
rv.C = C.LLVMConstSelect(cond.C, iftrue.C, iffalse.C)
return
}
3 changes: 3 additions & 0 deletions transforms_ipo.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build !llvm17
// +build !llvm17

//===- transforms_ipo.go - Bindings for ipo -------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
Expand Down
3 changes: 3 additions & 0 deletions transforms_pmbuilder.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build !llvm17
// +build !llvm17

//===- transforms_pmbuilder.go - Bindings for PassManagerBuilder ----------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
Expand Down
3 changes: 3 additions & 0 deletions transforms_scalar.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build !llvm17
// +build !llvm17

//===- transforms_scalar.go - Bindings for scalaropts ---------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
Expand Down

0 comments on commit 4428c1b

Please sign in to comment.