Skip to content

Commit

Permalink
update to LLVM 16
Browse files Browse the repository at this point in the history
  • Loading branch information
terencode committed Dec 21, 2024
1 parent c5fd645 commit 6c75169
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 32 deletions.
20 changes: 9 additions & 11 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,35 @@ on:

jobs:
build:
runs-on: ubuntu-latest
runs-on: ubuntu-22.04

steps:
- uses: kenchan0130/actions-system-info@master
- uses: kenchan0130/actions-system-info@v1.3.0
id: system-info
# Checks-out the repository
- uses: actions/checkout@v3
- uses: actions/checkout@v4


- name: setup llvm 15 repo
- name: setup llvm 16 repo
run: |
echo "deb http://apt.llvm.org/focal/ llvm-toolchain-focal-15 main" | sudo tee -a /etc/apt/sources.list
echo "deb-src http://apt.llvm.org/focal/ llvm-toolchain-focal-15 main" | sudo tee -a /etc/apt/sources.list
echo "deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-16 main" | sudo tee -a /etc/apt/sources.list
echo "deb-src http://apt.llvm.org/jammy/ llvm-toolchain-jammy-16 main" | sudo tee -a /etc/apt/sources.list
wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key|sudo apt-key add -
sudo apt update
- name: Set up OCaml
# You may pin to the exact commit or the version.
# uses: ocaml/setup-ocaml@6d924c1a7769aa5cdd74bdd901f6e24eb05024b1
uses: ocaml/setup-ocaml@v2
uses: ocaml/setup-ocaml@v3
with:
ocaml-compiler: 5.1.X
ocaml-compiler: 5


- run: opam install . --deps-only

- run: opam exec -- dune build

- name: Archive sailor
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: sailor for ${{ steps.system-info.outputs.release }}
path: |
Expand Down
2 changes: 1 addition & 1 deletion dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@
(logs (>= 0.7))
(mtime (>= 1.3.0))
(ctypes-foreign (>= 0.18.0))
(llvm (= 15.0.7+nnp-2))
(llvm (= 16.0.6+nnp))
zarith
))
2 changes: 1 addition & 1 deletion sail-pl.opam
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ depends: [
"logs" {>= "0.7"}
"mtime" {>= "1.3.0"}
"ctypes-foreign" {>= "0.18.0"}
"llvm" {= "15.0.7+nnp-2"}
"llvm" {= "16.0.6+nnp"}
"zarith"
"odoc" {with-doc}
]
Expand Down
4 changes: 2 additions & 2 deletions src/codegen/codegenEnv.ml
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ let getLLVMBasicType f ty llc llm : L.lltype E.t =
| Int n -> L.integer_type llc n |> return
| Float -> L.double_type llc |> return
| Char -> L.i8_type llc |> return
| String -> L.pointer_type2 llc |> return
| String -> L.pointer_type llc |> return
| ArrayType (t,s) -> let+ t = aux t in L.array_type t s
| Box _ | RefType _ -> L.pointer_type2 llc |> return
| Box _ | RefType _ -> L.pointer_type llc |> return
| GenericType _ -> E.throw Logging.(make_msg ty.loc "no generic type in codegen")
| CompoundType {name; _} when name.value = "_value" -> L.i64_type llc |> return (* for extern functions *)
| CompoundType {origin=None;_}
Expand Down
23 changes: 13 additions & 10 deletions src/codegen/codegenUtils.ml
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
open Llvm
open Common
module L = Llvm
open TypesCommon
open CodegenEnv
open Monad.UseMonad(Logging.Logger)

type llvm_args = { c:llcontext; b:llbuilder;m:llmodule; layout : Llvm_target.DataLayout.t}
type llvm_args = { c:L.llcontext; b:L.llbuilder;m:L.llmodule; layout : Llvm_target.DataLayout.t}
let mangle_method_name (name:string) (mname:string) (args: sailtype list ) : string =
let back = List.fold_left (fun s t -> s ^ string_of_sailtype (Some t) ^ "_" ) "" args in
let front = "_" ^ mname ^ "_" ^ name ^ "_" in
let res = front ^ back in
(* Logs.debug (fun m -> m "renamed %s to %s" name res); *)
res

let getLLVMLiteral (l:literal) (llvm:llvm_args) : llvalue =
let getLLVMLiteral (l:literal) (llvm:llvm_args) : L.llvalue =
let open L in
match l with
| LBool b -> const_int (i1_type llvm.c) (Bool.to_int b)
| LInt i -> const_int_of_string (integer_type llvm.c i.size) (Z.to_string i.l) 10
| LFloat f -> const_float (double_type llvm.c) f
| LChar c -> const_int (i8_type llvm.c) (Char.code c)
| LString s -> let s = build_global_stringptr s ".str" llvm.b in build_pointercast s (pointer_type2 llvm.c) "" llvm.b
| LString s -> let s = build_global_stringptr s ".str" llvm.b in build_pointercast s (pointer_type llvm.c) "" llvm.b

let ty_of_alias(ty:sailtype) env : sailtype =
match ty.value with
Expand All @@ -31,7 +32,8 @@ let ty_of_alias(ty:sailtype) env : sailtype =
end
| _ -> ty

let unary (op:unOp) (t,v) : llbuilder -> llvalue =
let unary (op:unOp) (t,v) : L.llbuilder -> L.llvalue =
let open L in
let f =
match snd t,op with
| Float,Neg -> build_fneg
Expand All @@ -41,7 +43,8 @@ let unary (op:unOp) (t,v) : llbuilder -> llvalue =
in f v ""


let binary (op:binOp) (t:sailtype) (l1:llvalue) (l2:llvalue) : llbuilder -> llvalue =
let binary (op:binOp) (t:sailtype) (l1:L.llvalue) (l2:L.llvalue) : L.llbuilder -> L.llvalue =
let open L in
let operators = function
| Int _ ->
Some [
Expand Down Expand Up @@ -84,16 +87,16 @@ let binary (op:binOp) (t:sailtype) (l1:llvalue) (l2:llvalue) : llbuilder -> llva
| None -> Printf.sprintf "codegen: bad usage of binop '%s' with type %s" (string_of_binop op) (string_of_sailtype @@ Some t) |> failwith


let toLLVMArgs (args: param list ) (env:DeclEnv.t) (llvm:llvm_args) : (bool * sailtype * llvalue) array E.t =
let toLLVMArgs (args: param list ) (env:DeclEnv.t) (llvm:llvm_args) : (bool * sailtype * L.llvalue) array E.t =
ListM.map (
fun {id;mut;ty=t;_} ->
let+ ty = getLLVMType env t llvm.c llvm.m in
mut,t,build_alloca ty id llvm.b
mut,t,L.build_alloca ty id llvm.b
) args <&> Array.of_list


let get_memcpy_intrinsic llvm =
let args_type = [|pointer_type2 llvm.c; pointer_type2 llvm.c; i64_type llvm.c; i1_type llvm.c|] in

let open L in
let args_type = [|pointer_type llvm.c; pointer_type llvm.c; i64_type llvm.c; i1_type llvm.c|] in
let f = declare_function "llvm.memcpy.p0i8.p0i8.i64" (function_type (void_type llvm.c) args_type ) llvm.m in
f
14 changes: 7 additions & 7 deletions src/codegen/codegen_.ml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ let rec eval_l (venv,tenv as env:SailEnv.t* Env.TypeEnv.t) (llvm:llvm_args) (exp
Env.TypeEnv.get_from_id (mk_locatable a.array.tag.loc a.array.tag.ty) tenv >>= fun t ->
getLLVMType (snd venv) t llvm.c llvm.m in
let+ index = eval_r env llvm a.idx in
let llvm_array = L.build_in_bounds_gep2 llty array_val [|L.(const_int (i64_type llvm.c) 0 ); index|] "" llvm.b in
let llvm_array = L.build_in_bounds_gep llty array_val [|L.(const_int (i64_type llvm.c) 0 ); index|] "" llvm.b in
llvm_array

| StructRead2 s ->
Expand All @@ -42,7 +42,7 @@ let rec eval_l (venv,tenv as env:SailEnv.t* Env.TypeEnv.t) (llvm:llvm_args) (exp

let fields = decl.defn.fields in
let {value=_,idx;_} = List.assoc s.value.field.value fields in
L.build_struct_gep2 llty st idx "" llvm.b
L.build_struct_gep llty st idx "" llvm.b

| StructAlloc2 s ->
let _,fieldlist = s.value.fields |> List.split in
Expand All @@ -54,7 +54,7 @@ let rec eval_l (venv,tenv as env:SailEnv.t* Env.TypeEnv.t) (llvm:llvm_args) (exp
let struct_v = L.build_alloca strct_ty "" llvm.b in
let+ () = ListM.iteri ( fun i f ->
let+ v = eval_r env llvm f.value in
let v_f = L.build_struct_gep2 (L.pointer_type2 llvm.c) struct_v i "" llvm.b in
let v_f = L.build_struct_gep (L.pointer_type llvm.c) struct_v i "" llvm.b in
L.build_store v v_f llvm.b |> ignore
) fieldlist in
struct_v
Expand All @@ -67,7 +67,7 @@ and eval_r (venv,tenv as env:SailEnv.t* Env.TypeEnv.t) (llvm:llvm_args) (exp:Mir
let* ty = Env.TypeEnv.get_from_id (mk_locatable exp.tag.loc exp.tag.ty) tenv in
let* llty = getLLVMType (snd venv) ty llvm.c llvm.m in
match exp.node with
| Variable _ | StructRead2 _ | ArrayRead _ | StructAlloc2 _ -> let+ v = eval_l env llvm exp in L.build_load2 llty v "" llvm.b
| Variable _ | StructRead2 _ | ArrayRead _ | StructAlloc2 _ -> let+ v = eval_l env llvm exp in L.build_load llty v "" llvm.b

| Literal l -> return @@ getLLVMLiteral l llvm

Expand All @@ -79,7 +79,7 @@ and eval_r (venv,tenv as env:SailEnv.t* Env.TypeEnv.t) (llvm:llvm_args) (exp:Mir
in binary bop.op (ty_of_alias ty (snd venv)) l1 l2 llvm.b
| Ref (_,e) -> eval_l env llvm e

| Deref e -> let+ v = eval_l env llvm e in L.build_load2 llty v "" llvm.b
| Deref e -> let+ v = eval_l env llvm e in L.build_load llty v "" llvm.b

| ArrayStatic elements ->
begin
Expand All @@ -92,7 +92,7 @@ and eval_r (venv,tenv as env:SailEnv.t* Env.TypeEnv.t) (llvm:llvm_args) (exp:Mir
L.set_linkage L.Linkage.Private array;
L.set_unnamed_addr true array;
L.set_global_constant true array;
L.build_load2 llty array "" llvm.b
L.build_load llty array "" llvm.b
end

| EnumAlloc _ -> E.throw Logging.(make_msg exp.tag.loc "enum allocation unimplemented")
Expand Down Expand Up @@ -131,7 +131,7 @@ and construct_call (name:string) (mname:l_str) (args:MirAst.expression list) (ve
else
return llargs
in
L.build_call2 llty llval (Array.of_list args) "" llvm.b
L.build_call llty llval (Array.of_list args) "" llvm.b

open MirAst

Expand Down

0 comments on commit 6c75169

Please sign in to comment.