Skip to content

Update to llvm 20 #25

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

Merged
merged 3 commits into from
Apr 1, 2025
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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
compile_commands.json
doc
.clangd
.vscode/
13 changes: 0 additions & 13 deletions .gitlab-ci.yml

This file was deleted.

13 changes: 7 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[package]
name = "tblgen"
version = "0.5.2"
edition = "2021"
version = "0.6.0"
edition = "2024"
license = "MIT OR Apache-2.0"
authors = ["Daan Vanoverloop"]
authors = ["Daan Vanoverloop", "Yota Toyama", "Edgar Luque"]
description = "Safe Rust bindings for TableGen."
repository = "https://github.com/mlir-rs/tblgen-rs"
keywords = ["llvm", "tablegen", "bindings", "wrapper"]
Expand All @@ -12,16 +12,17 @@ documentation = "https://mlir-rs.github.io/tblgen-rs/tblgen/"
exclude = ["doc/"]

[features]
default = ["llvm19-0"]
default = ["llvm20-0"]
llvm16-0 = []
llvm17-0 = []
llvm18-0 = []
llvm19-0 = []
llvm20-0 = []

[dependencies]
thiserror = "2.0.11"
thiserror = "2.0.12"
paste = "1.0.15"

[build-dependencies]
bindgen = "0.71.1"
cc = "1.2.9"
cc = "1.2.17"
10 changes: 6 additions & 4 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
ffi::OsStr,
fs::read_dir,
path::Path,
process::{exit, Command},
process::{Command, exit},
str,
};

Expand All @@ -14,8 +14,10 @@ const LLVM_MAJOR_VERSION: usize = if cfg!(feature = "llvm16-0") {
17
} else if cfg!(feature = "llvm18-0") {
18
} else {
} else if cfg!(feature = "llvm19-0") {
19
} else {
20
};

fn main() {
Expand Down Expand Up @@ -82,8 +84,8 @@ fn run() -> Result<(), Box<dyn Error>> {
}

fn build_c_library() -> Result<(), Box<dyn Error>> {
env::set_var("CXXFLAGS", llvm_config("--cxxflags")?);
env::set_var("CFLAGS", llvm_config("--cflags")?);
unsafe { env::set_var("CXXFLAGS", llvm_config("--cxxflags")?) };
unsafe { env::set_var("CFLAGS", llvm_config("--cflags")?) };

cc::Build::new()
.cpp(true)
Expand Down
4 changes: 2 additions & 2 deletions cc/lib/TableGen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ using namespace llvm;
namespace ctablegen {

typedef std::map<std::string, std::unique_ptr<Record>, std::less<>> RecordMap;
typedef std::vector<Record *> RecordVector;
typedef std::vector<const Record *> RecordVector;
typedef std::pair<std::string, TypedInit *> DagPair;

class TableGenParser {
Expand All @@ -50,7 +50,7 @@ class TableGenParser {
};

// Utility
TableGenRecTyKind tableGenFromRecType(RecTy *rt);
TableGenRecTyKind tableGenFromRecType(const RecTy *rt);

/// A simple raw ostream subclass that forwards write_impl calls to the
/// user-supplied callback together with opaque user-supplied data.
Expand Down
11 changes: 6 additions & 5 deletions cc/lib/Utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
#include "TableGen.hpp"
#include "Types.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/TableGen/Error.h"

namespace ctablegen {

TableGenRecTyKind tableGenFromRecType(RecTy *rt) {
TableGenRecTyKind tableGenFromRecType(const RecTy *rt) {
switch (rt->getRecTyKind()) {
case RecTy::BitRecTyKind:
return TableGenBitRecTyKind;
Expand Down Expand Up @@ -69,7 +68,8 @@ int8_t *tableGenBitsInitGetValue(TableGenTypedInitRef ti, size_t *len) {
auto bits = new int8_t[*len];

for (size_t i = 0; i < *len; i++) {
bits[i] = reinterpret_cast<BitInit *>(bits_init->getBit(i))->getValue();
bits[i] =
reinterpret_cast<const BitInit *>(bits_init->getBit(i))->getValue();
}

return bits;
Expand All @@ -94,7 +94,7 @@ TableGenTypedInitRef tableGenBitsInitGetBitInit(TableGenTypedInitRef ti,
if (!bits_init)
return nullptr;

return wrap(static_cast<BitInit *>(bits_init->getBit(index)));
return wrap(static_cast<const BitInit *>(bits_init->getBit(index)));
}

TableGenBool tableGenIntInitGetValue(TableGenTypedInitRef ti,
Expand Down Expand Up @@ -181,7 +181,8 @@ TableGenBool tableGenPrintError(TableGenParserRef ref,
}

TableGenSourceLocationRef tableGenSourceLocationNull() {
return wrap(new ArrayRef(SMLoc()));
auto source_loc = SMLoc();
return wrap(new ArrayRef(source_loc));
}

TableGenSourceLocationRef
Expand Down
8 changes: 4 additions & 4 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,20 @@

use std::{
convert::Infallible,
ffi::{c_void, NulError},
ffi::{NulError, c_void},
fmt::{self, Display, Formatter},
str::Utf8Error,
string::FromUtf8Error,
};

use crate::{
SourceInfo, TableGenParser,
raw::{
tableGenPrintError, tableGenSourceLocationClone, tableGenSourceLocationFree,
tableGenSourceLocationNull, TableGenDiagKind::TABLEGEN_DK_ERROR, TableGenSourceLocationRef,
TableGenDiagKind::TABLEGEN_DK_ERROR, TableGenSourceLocationRef, tableGenPrintError,
tableGenSourceLocationClone, tableGenSourceLocationFree, tableGenSourceLocationNull,
},
string_ref::StringRef,
util::print_string_callback,
SourceInfo, TableGenParser,
};

/// Enum of TableGen errors.
Expand Down
36 changes: 19 additions & 17 deletions src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@

use crate::{
raw::{
tableGenBitInitGetValue, tableGenBitsInitGetBitInit, tableGenBitsInitGetNumBits,
tableGenDagRecordArgName, tableGenDagRecordGet, tableGenDagRecordNumArgs,
tableGenDagRecordOperator, tableGenDefInitGetValue, tableGenInitPrint, tableGenInitRecType,
tableGenIntInitGetValue, tableGenListRecordGet, tableGenListRecordNumElements,
tableGenStringInitGetValue, TableGenRecTyKind, TableGenTypedInitRef,
TableGenRecTyKind, TableGenTypedInitRef, tableGenBitInitGetValue,
tableGenBitsInitGetBitInit, tableGenBitsInitGetNumBits, tableGenDagRecordArgName,
tableGenDagRecordGet, tableGenDagRecordNumArgs, tableGenDagRecordOperator,
tableGenDefInitGetValue, tableGenInitPrint, tableGenInitRecType, tableGenIntInitGetValue,
tableGenListRecordGet, tableGenListRecordNumElements, tableGenStringInitGetValue,
},
string_ref::StringRef,
util::print_callback,
Expand Down Expand Up @@ -197,18 +197,20 @@ impl<'a> TypedInit<'a> {
/// The raw object must be valid.
#[allow(non_upper_case_globals)]
pub unsafe fn from_raw(init: TableGenTypedInitRef) -> Self {
let t = tableGenInitRecType(init);

use TableGenRecTyKind::*;
match t {
TableGenBitRecTyKind => Self::Bit(BitInit::from_raw(init)),
TableGenBitsRecTyKind => Self::Bits(BitsInit::from_raw(init)),
TableGenDagRecTyKind => TypedInit::Dag(DagInit::from_raw(init)),
TableGenIntRecTyKind => TypedInit::Int(IntInit::from_raw(init)),
TableGenListRecTyKind => TypedInit::List(ListInit::from_raw(init)),
TableGenRecordRecTyKind => Self::Def(DefInit::from_raw(init)),
TableGenStringRecTyKind => Self::String(StringInit::from_raw(init)),
_ => Self::Invalid,
unsafe {
let t = tableGenInitRecType(init);

use TableGenRecTyKind::*;
match t {
TableGenBitRecTyKind => Self::Bit(BitInit::from_raw(init)),
TableGenBitsRecTyKind => Self::Bits(BitsInit::from_raw(init)),
TableGenDagRecTyKind => TypedInit::Dag(DagInit::from_raw(init)),
TableGenIntRecTyKind => TypedInit::Int(IntInit::from_raw(init)),
TableGenListRecTyKind => TypedInit::List(ListInit::from_raw(init)),
TableGenRecordRecTyKind => Self::Def(DefInit::from_raw(init)),
TableGenStringRecTyKind => Self::String(StringInit::from_raw(init)),
_ => Self::Invalid,
}
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let keeper: RecordKeeper = TableGenParser::new()
//! .add_source(r#"include "mlir/IR/OpBase.td""#)?
//! .add_include_directory(&format!("{}/include", std::env::var("TABLEGEN_190_PREFIX")?))
//! .add_include_directory(&format!("{}/include", std::env::var("TABLEGEN_200_PREFIX")?))
//! .parse()?;
//! let i32_def = keeper.def("I32").expect("has I32 def");
//! assert!(i32_def.subclass_of("I"));
Expand All @@ -80,7 +80,7 @@
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let keeper: RecordKeeper = TableGenParser::new()
//! .add_source_file("mlir/IR/OpBase.td")
//! .add_include_directory(&format!("{}/include", std::env::var("TABLEGEN_190_PREFIX")?))
//! .add_include_directory(&format!("{}/include", std::env::var("TABLEGEN_200_PREFIX")?))
//! .parse()?;
//! let i32_def = keeper.def("I32").expect("has I32 def");
//! assert!(i32_def.subclass_of("I"));
Expand Down Expand Up @@ -126,8 +126,8 @@ pub use record::RecordValue;
pub use record_keeper::RecordKeeper;

use raw::{
tableGenAddIncludeDirectory, tableGenAddSource, tableGenAddSourceFile, tableGenFree,
tableGenGet, tableGenParse, TableGenParserRef,
TableGenParserRef, tableGenAddIncludeDirectory, tableGenAddSource, tableGenAddSourceFile,
tableGenFree, tableGenGet, tableGenParse,
};
use string_ref::StringRef;

Expand Down
14 changes: 7 additions & 7 deletions src/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ use std::ffi::c_void;
use std::marker::PhantomData;

use crate::raw::{
tableGenRecordGetFirstValue, tableGenRecordGetLoc, tableGenRecordGetName,
tableGenRecordGetValue, tableGenRecordIsAnonymous, tableGenRecordIsSubclassOf,
tableGenRecordPrint, tableGenRecordValGetLoc, tableGenRecordValGetNameInit,
tableGenRecordValGetValue, tableGenRecordValNext, tableGenRecordValPrint, TableGenRecordRef,
TableGenRecordValRef,
TableGenRecordRef, TableGenRecordValRef, tableGenRecordGetFirstValue, tableGenRecordGetLoc,
tableGenRecordGetName, tableGenRecordGetValue, tableGenRecordIsAnonymous,
tableGenRecordIsSubclassOf, tableGenRecordPrint, tableGenRecordValGetLoc,
tableGenRecordValGetNameInit, tableGenRecordValGetValue, tableGenRecordValNext,
tableGenRecordValPrint,
};

use crate::error::{Error, SourceLoc, SourceLocation, TableGenError, WithLocation};
Expand Down Expand Up @@ -259,8 +259,8 @@ impl RecordValue<'_> {
///
/// The raw object must be valid.
pub unsafe fn from_raw(ptr: TableGenRecordValRef) -> Self {
let name = StringInit::from_raw(tableGenRecordValGetNameInit(ptr));
let value = TypedInit::from_raw(tableGenRecordValGetValue(ptr));
let name = unsafe { StringInit::from_raw(tableGenRecordValGetNameInit(ptr)) };
let value = unsafe { TypedInit::from_raw(tableGenRecordValGetValue(ptr)) };
Self {
name,
init: value,
Expand Down
12 changes: 8 additions & 4 deletions src/record_keeper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@

use std::marker::PhantomData;

#[cfg(any(feature = "llvm18-0", feature = "llvm19-0"))]
#[cfg(any(feature = "llvm18-0", feature = "llvm19-0", feature = "llvm20-0"))]
use crate::error::TableGenError;
#[cfg(any(feature = "llvm16-0", feature = "llvm17-0"))]
use crate::error::{SourceLocation, TableGenError, WithLocation};
use crate::raw::{
TableGenRecordKeeperIteratorRef, TableGenRecordKeeperRef, TableGenRecordVectorRef,
tableGenRecordKeeperFree, tableGenRecordKeeperGetAllDerivedDefinitions,
tableGenRecordKeeperGetClass, tableGenRecordKeeperGetDef, tableGenRecordKeeperGetFirstClass,
tableGenRecordKeeperGetFirstDef, tableGenRecordKeeperGetNextClass,
tableGenRecordKeeperGetNextDef, tableGenRecordKeeperItemGetName,
tableGenRecordKeeperItemGetRecord, tableGenRecordKeeperIteratorClone,
tableGenRecordKeeperIteratorFree, tableGenRecordVectorFree, tableGenRecordVectorGet,
TableGenRecordKeeperIteratorRef, TableGenRecordKeeperRef, TableGenRecordVectorRef,
};
use crate::record::Record;
use crate::string_ref::StringRef;
Expand Down Expand Up @@ -115,13 +115,17 @@ trait NextRecord {

impl NextRecord for IsClass {
unsafe fn next(raw: &mut TableGenRecordKeeperIteratorRef) {
tableGenRecordKeeperGetNextClass(raw);
unsafe {
tableGenRecordKeeperGetNextClass(raw);
}
}
}

impl NextRecord for IsDef {
unsafe fn next(raw: &mut TableGenRecordKeeperIteratorRef) {
tableGenRecordKeeperGetNextDef(raw);
unsafe {
tableGenRecordKeeperGetNextDef(raw);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/string_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl StringRef<'_> {

pub unsafe fn from_option_raw(raw: TableGenStringRef) -> Option<Self> {
if !raw.data.is_null() {
Some(Self::from_raw(raw))
Some(unsafe { Self::from_raw(raw) })
} else {
None
}
Expand Down
11 changes: 7 additions & 4 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
use crate::{error::TableGenError, raw::TableGenStringRef, string_ref::StringRef};

pub(crate) unsafe extern "C" fn print_callback(string: TableGenStringRef, data: *mut c_void) {
let (formatter, result) = &mut *(data as *mut (&mut Formatter, fmt::Result));
let (formatter, result) = unsafe { &mut *(data as *mut (&mut Formatter, fmt::Result)) };

if result.is_err() {
return;
Expand All @@ -16,7 +16,8 @@ pub(crate) unsafe extern "C" fn print_callback(string: TableGenStringRef, data:
write!(
formatter,
"{}",
TryInto::<&str>::try_into(StringRef::from_raw(string)).map_err(|_| fmt::Error)?
TryInto::<&str>::try_into(unsafe { StringRef::from_raw(string) })
.map_err(|_| fmt::Error)?
)
})();
}
Expand All @@ -25,14 +26,16 @@ pub(crate) unsafe extern "C" fn print_string_callback(
string: TableGenStringRef,
data: *mut c_void,
) {
let (writer, result) = &mut *(data as *mut (String, Result<(), TableGenError>));
let (writer, result) = unsafe { &mut *(data as *mut (String, Result<(), TableGenError>)) };

if result.is_err() {
return;
}

*result = (|| {
writer.push_str(StringRef::from_raw(string).as_str()?);
unsafe {
writer.push_str(StringRef::from_raw(string).as_str()?);
}

Ok(())
})();
Expand Down
4 changes: 2 additions & 2 deletions tools/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

set -e

llvm_version=19
llvm_version=20

brew update
brew install llvm@$llvm_version

llvm_prefix=$(brew --prefix llvm@$llvm_version)

echo TABLEGEN_190_PREFIX=$llvm_prefix >>$GITHUB_ENV
echo TABLEGEN_200_PREFIX=$llvm_prefix >>$GITHUB_ENV
echo PATH=$llvm_prefix/bin:$PATH >>$GITHUB_ENV
echo LIBRARY_PATH=$llvm_prefix/lib:$LIBRARY_PATH >>$GITHUB_ENV
echo LD_LIBRARY_PATH=$llvm_prefix/lib:$LD_LIBRARY_PATH >>$GITHUB_ENV
Loading