Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to support LLVM 17 #55

Merged
merged 7 commits into from
Jan 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jobs:
- 14
- 15
- 16
- 17

steps:
- name: Checkout sources
Expand Down
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"rust-analyzer.cargo.features": [
"llvm-16"
"llvm-17"
]
}
8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ llvm-sys-130 = { package = "llvm-sys", version = "130.0.0", optional = true }
llvm-sys-140 = { package = "llvm-sys", version = "140.0.0", optional = true }
llvm-sys-150 = { package = "llvm-sys", version = "150.1.0", optional = true }
llvm-sys-160 = { package = "llvm-sys", version = "160.1.2", optional = true }
llvm-sys-170 = { package = "llvm-sys", version = "170.0.1", optional = true }
either = "1.9"
log = "0.4"

Expand All @@ -40,6 +41,7 @@ llvm-13 = ["llvm-sys-130", "llvm-13-or-lower", "llvm-13-or-greater"]
llvm-14 = ["llvm-sys-140", "llvm-14-or-lower", "llvm-14-or-greater"]
llvm-15 = ["llvm-sys-150", "llvm-15-or-lower", "llvm-15-or-greater"]
llvm-16 = ["llvm-sys-160", "llvm-16-or-lower", "llvm-16-or-greater"]
llvm-17 = ["llvm-sys-170", "llvm-17-or-lower", "llvm-17-or-greater"]

###
# For convenience, these automatically-enabled features allow us to avoid
Expand All @@ -54,6 +56,7 @@ llvm-13-or-greater = ["llvm-12-or-greater"]
llvm-14-or-greater = ["llvm-13-or-greater"]
llvm-15-or-greater = ["llvm-14-or-greater"]
llvm-16-or-greater = ["llvm-15-or-greater"]
llvm-17-or-greater = ["llvm-16-or-greater"]

llvm-8-or-lower = ["llvm-9-or-lower"]
llvm-9-or-lower = ["llvm-10-or-lower"]
Expand All @@ -63,7 +66,8 @@ llvm-12-or-lower = ["llvm-13-or-lower"]
llvm-13-or-lower = ["llvm-14-or-lower"]
llvm-14-or-lower = ["llvm-15-or-lower"]
llvm-15-or-lower = ["llvm-16-or-lower"]
llvm-16-or-lower = []
llvm-16-or-lower = ["llvm-17-or-lower"]
llvm-17-or-lower = []
###

# We'd like to have a "strict-versioning" feature which enables the
Expand Down Expand Up @@ -94,6 +98,7 @@ llvm-13-strict = ["llvm-13", "llvm-sys-130/strict-versioning"]
llvm-14-strict = ["llvm-14", "llvm-sys-140/strict-versioning"]
llvm-15-strict = ["llvm-15", "llvm-sys-150/strict-versioning"]
llvm-16-strict = ["llvm-16", "llvm-sys-160/strict-versioning"]
llvm-17-strict = ["llvm-17", "llvm-sys-170/strict-versioning"]

# As with the strict feature above, we cannot enable it globally, as it would
# activate all the llvm-sys-* dependencies, so have a single feature for each
Expand All @@ -104,6 +109,7 @@ llvm-13-dynamic = ["llvm-13", "llvm-sys-130/prefer-dynamic"]
llvm-14-dynamic = ["llvm-14", "llvm-sys-140/prefer-dynamic"]
llvm-15-dynamic = ["llvm-15", "llvm-sys-150/prefer-dynamic"]
llvm-16-dynamic = ["llvm-16", "llvm-sys-160/prefer-dynamic"]
llvm-17-dynamic = ["llvm-17", "llvm-sys-170/prefer-dynamic"]

[package.metadata.docs.rs]
# Generate docs.rs documentation with the llvm-10 feature
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ add it as a dependency in your `Cargo.toml`, selecting the feature corresponding
to the LLVM version you want:
```toml
[dependencies]
llvm-ir = { version = "0.10.0", features = ["llvm-16"] }
llvm-ir = { version = "0.10.0", features = ["llvm-17"] }
```

Currently, the supported LLVM versions are `llvm-8`, `llvm-9`, `llvm-10`,
`llvm-11`, `llvm-12`, `llvm-13`, `llvm-14`, `llvm-15`, and `llvm-16`.
`llvm-11`, `llvm-12`, `llvm-13`, `llvm-14`, `llvm-15`, `llvm-16`, and `llvm-17`.

Then, the easiest way to get started is to parse some existing LLVM IR into
this crate's data structures.
Expand Down Expand Up @@ -82,8 +82,8 @@ using.
## Compatibility
Starting with `llvm-ir` 0.7.0, LLVM versions are selected by a Cargo feature
flag. This means that a single crate version can be used for any supported LLVM
version. Currently, `llvm-ir` supports LLVM versions 8 through 16, selected by
feature flags `llvm-8` through `llvm-16`.
version. Currently, `llvm-ir` supports LLVM versions 8 through 17, selected by
feature flags `llvm-8` through `llvm-17`.

`llvm-ir` works on stable Rust, and requires Rust 1.45+.

Expand Down
3 changes: 3 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ fn main() {
if cfg!(feature = "llvm-16") {
versions.push(16);
}
if cfg!(feature = "llvm-17") {
versions.push(17);
}
match versions.len() {
0 => panic!("llvm-ir: Please select an LLVM version using a Cargo feature."),
1 => {},
Expand Down
4 changes: 2 additions & 2 deletions refresh-docs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
# Requires ghp-import to be installed (e.g. via pip3)

rm -rf target/doc && # purge old docs that may include docs for deps
cargo doc --no-deps --features=llvm-16 && # document just this crate
cargo doc --no-deps --features=llvm-17 && # document just this crate
echo "<meta http-equiv=refresh content=0;url=llvm_ir/index.html>" > target/doc/index.html && # put in the top-level redirect
ghp-import -np target/doc && # publish to gh-pages branch
rm -rf target/doc && # kill the docs that were just this crate
cargo doc --features=llvm-16 # regenerate all docs (including deps) for local use
cargo doc --features=llvm-17 # regenerate all docs (including deps) for local use
9 changes: 9 additions & 0 deletions src/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ pub enum Constant {
// Other ops
ICmp(ICmp),
FCmp(FCmp),
#[cfg(feature = "llvm-16-or-lower")]
Select(Select),
}

Expand Down Expand Up @@ -263,6 +264,7 @@ impl Typed for Constant {
Constant::AddrSpaceCast(a) => types.type_of(a),
Constant::ICmp(i) => types.type_of(i),
Constant::FCmp(f) => types.type_of(f),
#[cfg(feature="llvm-16-or-lower")]
Constant::Select(s) => types.type_of(s),
}
}
Expand Down Expand Up @@ -431,6 +433,7 @@ impl Display for Constant {
Constant::AddrSpaceCast(a) => write!(f, "{}", a),
Constant::ICmp(i) => write!(f, "{}", i),
Constant::FCmp(c) => write!(f, "{}", c),
#[cfg(feature="llvm-16-or-lower")]
Constant::Select(s) => write!(f, "{}", s),
}
}
Expand Down Expand Up @@ -1262,15 +1265,18 @@ impl Display for FCmp {
}
}

#[cfg(feature="llvm-16-or-lower")]
#[derive(PartialEq, Clone, Debug)]
pub struct Select {
pub condition: ConstantRef,
pub true_value: ConstantRef,
pub false_value: ConstantRef,
}

#[cfg(feature="llvm-16-or-lower")]
impl_constexpr!(Select, Select);

#[cfg(feature="llvm-16-or-lower")]
impl Typed for Select {
fn get_type(&self, types: &Types) -> TypeRef {
let t = types.type_of(&self.true_value);
Expand All @@ -1279,6 +1285,7 @@ impl Typed for Select {
}
}

#[cfg(feature="llvm-16-or-lower")]
impl Display for Select {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
Expand Down Expand Up @@ -1493,6 +1500,7 @@ impl Constant {
LLVMOpcode::LLVMAddrSpaceCast => Constant::AddrSpaceCast(AddrSpaceCast::from_llvm_ref(constant, ctx)),
LLVMOpcode::LLVMICmp => Constant::ICmp(ICmp::from_llvm_ref(constant, ctx)),
LLVMOpcode::LLVMFCmp => Constant::FCmp(FCmp::from_llvm_ref(constant, ctx)),
#[cfg(feature="llvm-16-or-lower")]
LLVMOpcode::LLVMSelect => Constant::Select(Select::from_llvm_ref(constant, ctx)),
opcode => panic!("ConstantExpr has unexpected opcode {:?}", opcode),
}
Expand Down Expand Up @@ -1691,6 +1699,7 @@ impl FCmp {
}
}

#[cfg(feature="llvm-16-or-lower")]
impl Select {
pub(crate) fn from_llvm_ref(expr: LLVMValueRef, ctx: &mut ModuleContext) -> Self {
assert_eq!(unsafe { LLVMGetNumOperands(expr) }, 3);
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,6 @@ pub fn llvm_version() -> &'static str {
case!("llvm-14");
case!("llvm-15");
case!("llvm-16");
case!("llvm-17");
unreachable!()
}
2 changes: 2 additions & 0 deletions src/llvm_sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ pub use llvm_sys_140 as llvm_sys;
pub use llvm_sys_150 as llvm_sys;
#[cfg(feature = "llvm-16")]
pub use llvm_sys_160 as llvm_sys;
#[cfg(feature = "llvm-17")]
pub use llvm_sys_170 as llvm_sys;
#[cfg(feature = "llvm-8")]
pub use llvm_sys_80 as llvm_sys;
#[cfg(feature = "llvm-9")]
Expand Down
11 changes: 10 additions & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1048,7 +1048,16 @@ impl TypesBuilder {
},
LLVMTypeKind::LLVMArrayTypeKind => {
let element_type = self.type_from_llvm_ref(unsafe { LLVMGetElementType(ty) });
self.array_of(element_type, unsafe { LLVMGetArrayLength(ty) as usize })

// LLVMGetArrayLength2 was added in LLVM-17: the old function still exists there,
// but is deprecated. The parameters are the same, but the return type is changed
// from c_uint to u64
#[cfg(feature = "llvm-16-or-lower")]
let array_len = unsafe { LLVMGetArrayLength(ty) as usize };
#[cfg(feature = "llvm-17-or-greater")]
let array_len = unsafe { LLVMGetArrayLength2(ty) as usize };

self.array_of(element_type, array_len)
},
LLVMTypeKind::LLVMVectorTypeKind => {
let element_type = self.type_from_llvm_ref(unsafe { LLVMGetElementType(ty) });
Expand Down
54 changes: 45 additions & 9 deletions tests/basic_bc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CLANG13=clang-13
CLANG14=clang-14
CLANG15=clang-15
CLANG16=$(LLVM16PATH)/bin/clang
CLANG17=$(LLVM17PATH)/bin/clang
CLANGXX8=clang++-8
CLANGXX9=clang++-9
CLANGXX10=clang++-10 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk
Expand All @@ -16,6 +17,7 @@ CLANGXX13=clang++-13
CLANGXX14=clang++-14
CLANGXX15=$(LLVM15PATH)/bin/clang++
CLANGXX16=$(LLVM16PATH)/bin/clang++
CLANGXX17=$(LLVM17PATH)/bin/clang++
RUSTC=rustc
RUSTFLAGS=--crate-type=lib
LLVMAS8=$(LLVM8PATH)/bin/llvm-as
Expand All @@ -27,6 +29,7 @@ LLVMAS13=$(LLVM13PATH)/bin/llvm-as
LLVMAS14=$(LLVM14PATH)/bin/llvm-as
LLVMAS15=$(LLVM15PATH)/bin/llvm-as
LLVMAS16=$(LLVM16PATH)/bin/llvm-as
LLVMAS17=$(LLVM17PATH)/bin/llvm-as

COBJS = \
hello.bc hello.ll hello.bc-g hello.ll-g \
Expand Down Expand Up @@ -55,6 +58,7 @@ all: \
$(patsubst %,llvm14/%,$(COBJS)) \
$(patsubst %,llvm15/%,$(COBJS)) \
$(patsubst %,llvm16/%,$(COBJS)) \
$(patsubst %,llvm17/%,$(COBJS)) \
$(patsubst %,llvm8/%,$(LLOBJS)) \
$(patsubst %,llvm9/%,$(LLOBJS)) \
$(patsubst %,llvm10/%,$(LLOBJS)) \
Expand All @@ -64,9 +68,11 @@ all: \
$(patsubst %,llvm14/%,$(LLOBJS)) \
$(patsubst %,llvm15/%,$(LLOBJS)) \
$(patsubst %,llvm16/%,$(LLOBJS)) \
$(patsubst %,llvm17/%,$(LLOBJS)) \
$(patsubst %,rust/%,$(RUSTOBJS)) \
llvm11/float_types.bc llvm12/float_types.bc llvm13/float_types.bc \
llvm14/float_types.bc llvm15/float_types.bc llvm16/float_types.bc \
llvm17/float_types.bc \

.PHONY: cxx
cxx: \
Expand All @@ -79,6 +85,7 @@ cxx: \
$(patsubst %,cxx-llvm14/%,$(CXXOBJS)) \
$(patsubst %,cxx-llvm15/%,$(CXXOBJS)) \
$(patsubst %,cxx-llvm16/%,$(CXXOBJS)) \
$(patsubst %,cxx-llvm17/%,$(CXXOBJS)) \

# default to -O3 unless overridden by a more specific rule
%: CFLAGS := -O3
Expand Down Expand Up @@ -110,6 +117,10 @@ llvm15/%.ll : %.c
llvm16/%.ll : %.c
mkdir -p llvm16
$(CLANG16) $(CFLAGS) -S -emit-llvm $^ -o $@
llvm17/%.ll : %.c
mkdir -p llvm17
$(CLANG17) $(CFLAGS) -S -emit-llvm $^ -o $@


llvm8/%.bc : %.c
mkdir -p llvm8
Expand Down Expand Up @@ -138,34 +149,40 @@ llvm15/%.bc : %.c
llvm16/%.bc : %.c
mkdir -p llvm16
$(CLANG16) $(CFLAGS) -c -emit-llvm $^ -o $@
llvm17/%.bc : %.c
mkdir -p llvm17
$(CLANG17) $(CFLAGS) -c -emit-llvm $^ -o $@

cxx-llvm8/%.ll : %.cpp
mkdir -p llvm8
mkdir -p cxx-llvm8
$(CLANGXX8) $(CFLAGS) -S -emit-llvm $^ -o $@
cxx-llvm9/%.ll : %.cpp
mkdir -p llvm9
mkdir -p cxx-llvm9
$(CLANGXX9) $(CFLAGS) -S -emit-llvm $^ -o $@
cxx-llvm10/%.ll : %.cpp
mkdir -p llvm10
mkdir -p cxx-llvm10
$(CLANGXX10) $(CFLAGS) -S -emit-llvm $^ -o $@
cxx-llvm11/%.ll : %.cpp
mkdir -p llvm11
mkdir -p cxx-llvm11
$(CLANGXX11) $(CFLAGS) -S -emit-llvm $^ -o $@
cxx-llvm12/%.ll : %.cpp
mkdir -p llvm12
mkdir -p cxx-llvm12
$(CLANGXX12) $(CFLAGS) -S -emit-llvm $^ -o $@
cxx-llvm13/%.ll : %.cpp
mkdir -p llvm13
mkdir -p cxx-llvm13
$(CLANGXX13) $(CFLAGS) -S -emit-llvm $^ -o $@
cxx-llvm14/%.ll : %.cpp
mkdir -p llvm14
mkdir -p cxx-llvm14
$(CLANGXX14) $(CFLAGS) -S -emit-llvm $^ -o $@
cxx-llvm15/%.ll : %.cpp
mkdir -p llvm15
mkdir -p cxx-llvm15
$(CLANGXX15) $(CFLAGS) -S -emit-llvm $^ -o $@
cxx-llvm16/%.ll : %.cpp
mkdir -p llvm16
mkdir -p cxx-llvm16
$(CLANGXX16) $(CFLAGS) -S -emit-llvm $^ -o $@
cxx-llvm17/%.ll : %.cpp
mkdir -p cxx-llvm17
$(CLANGXX17) $(CFLAGS) -S -emit-llvm $^ -o $@

cxx-llvm8/%.bc : %.cpp
mkdir -p cxx-llvm8
Expand Down Expand Up @@ -194,6 +211,9 @@ cxx-llvm15/%.bc : %.cpp
cxx-llvm16/%.bc : %.cpp
mkdir -p cxx-llvm16
$(CLANGXX16) $(CFLAGS) -c -emit-llvm $^ -o $@
cxx-llvm17/%.bc : %.cpp
mkdir -p cxx-llvm17
$(CLANGXX17) $(CFLAGS) -c -emit-llvm $^ -o $@

rust/%.ll : %.rs
mkdir -p rust
Expand Down Expand Up @@ -231,6 +251,9 @@ llvm15/%.ll-g : %.c
llvm16/%.ll-g : %.c
mkdir -p llvm16
$(CLANG16) $(CFLAGS) -g -S -emit-llvm $^ -o $@
llvm17/%.ll-g : %.c
mkdir -p llvm17
$(CLANG17) $(CFLAGS) -g -S -emit-llvm $^ -o $@

llvm8/%.bc-g : %.c
mkdir -p llvm8
Expand Down Expand Up @@ -259,6 +282,9 @@ llvm15/%.bc-g : %.c
llvm16/%.bc-g : %.c
mkdir -p llvm16
$(CLANG16) $(CFLAGS) -g -c -emit-llvm $^ -o $@
llvm17/%.bc-g : %.c
mkdir -p llvm17
$(CLANG17) $(CFLAGS) -g -c -emit-llvm $^ -o $@

rust/%.ll-g : %.rs
mkdir -p rust
Expand All @@ -278,6 +304,7 @@ llvm13/linkedlist.% : CFLAGS := -O0
llvm14/linkedlist.% : CFLAGS := -O0
llvm15/linkedlist.% : CFLAGS := -O0
llvm16/linkedlist.% : CFLAGS := -O0
llvm17/linkedlist.% : CFLAGS := -O0

# assemble this one directly from .ll
llvm8/fences.ll.bc : fences.ll
Expand Down Expand Up @@ -307,6 +334,9 @@ llvm15/fences.ll.bc : fences.ll
llvm16/fences.ll.bc : fences.ll
mkdir -p llvm16
$(LLVMAS16) $< -o $@
llvm17/fences.ll.bc : fences.ll
mkdir -p llvm17
$(LLVMAS17) $< -o $@

# assemble this one directly from .ll as well
llvm8/param_and_func_attributes.ll.bc : param_and_func_attributes.ll
Expand Down Expand Up @@ -339,6 +369,9 @@ llvm15/param_and_func_attributes.ll.bc : param_and_func_attributes_llvm13+.ll
llvm16/param_and_func_attributes.ll.bc : param_and_func_attributes_llvm16+.ll
mkdir -p llvm16
$(LLVMAS16) $< -o $@
llvm17/param_and_func_attributes.ll.bc : param_and_func_attributes_llvm16+.ll
mkdir -p llvm17
$(LLVMAS17) $< -o $@

# assemble this one directly from .ll, and only for LLVM 11+
llvm11/float_types.bc : float_types.ll
Expand All @@ -359,6 +392,9 @@ llvm15/float_types.bc : float_types.ll
llvm16/float_types.bc : float_types.ll
mkdir -p llvm16
$(LLVMAS16) $< -o $@
llvm17/float_types.bc : float_types.ll
mkdir -p llvm17
$(LLVMAS17) $< -o $@

.PHONY: clean
clean:
Expand Down
Binary file added tests/basic_bc/cxx-llvm17/throw.bc
Binary file not shown.
Loading
Loading