Skip to content

Commit

Permalink
Bump CIRCT
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianschuiki committed Jul 12, 2022
1 parent d0c5a3a commit 789dbac
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 21 deletions.
2 changes: 1 addition & 1 deletion circt
Submodule circt updated 770 files
95 changes: 77 additions & 18 deletions src/circt-sys/build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,37 @@
// Copyright (c) 2016-2021 Fabian Schuiki

use std::env;
use std::path::PathBuf;
use std::{
env,
ffi::OsStr,
path::{Path, PathBuf},
process::Command,
};

fn target_env_is(name: &str) -> bool {
match env::var_os("CARGO_CFG_TARGET_ENV") {
Some(s) => s == name,
None => false,
}
}

fn target_os_is(name: &str) -> bool {
match env::var_os("CARGO_CFG_TARGET_OS") {
Some(s) => s == name,
None => false,
}
}

fn llvm_config_run(binary: impl AsRef<OsStr>, arg: &str) -> String {
let output = Command::new(binary)
.arg(arg)
.arg("--link-static")
.output()
.expect("llvm-config run failed");
String::from_utf8(output.stdout)
.expect("Output from llvm-config was not valid UTF-8")
.trim()
.to_string()
}

fn main() {
static ENV_CIRCT_DIR: &str = "CIRCT_SYS_CIRCT_DIR";
Expand All @@ -25,8 +55,21 @@ fn main() {
.map(PathBuf::from)
.unwrap_or_else(|_| llvm_dir.join("build"));

// Locate `llvm-config` to figure out what to link.
let llvm_config_bin = llvm_build_dir.join("bin").join("llvm-config");
let llvm_lib_dir = llvm_config_run(&llvm_config_bin, "--libdir");
let llvm_include_dir = llvm_config_run(&llvm_config_bin, "--includedir");

// Library search paths
let lib_dirs = [
circt_build_dir.join("lib"),
Path::new(llvm_lib_dir.as_str()).to_owned(),
];
for name in &lib_dirs {
println!("cargo:rustc-link-search=native={}", name.display());
}

// CIRCT/LLVM/MLIR libraries
let lib_dirs = [circt_build_dir.join("lib"), llvm_build_dir.join("lib")];
let lib_names = [
"CIRCTCAPIComb",
"CIRCTCAPIHW",
Expand All @@ -46,18 +89,17 @@ fn main() {
"LLVMRemarks",
"LLVMSupport",
"MLIRAnalysis",
"MLIRArithmetic",
"MLIRArithmeticDialect",
"MLIRCAPIFunc",
"MLIRCAPIIR",
"MLIRCAPIControlFlow",
"MLIRCallInterfaces",
"MLIRControlFlow",
"MLIRControlFlowDialect",
"MLIRControlFlowInterfaces",
"MLIRFunc",
"MLIRFuncDialect",
"MLIRIR",
"MLIRInferTypeOpInterface",
"MLIRPDL",
"MLIRPDLInterp",
"MLIRInferIntRangeInterface",
"MLIRPDLToPDLInterp",
"MLIRParser",
"MLIRPass",
Expand All @@ -67,12 +109,12 @@ fn main() {
"MLIRTransformUtils",
"MLIRTransforms",
];
for name in &lib_dirs {
println!("cargo:rustc-link-search=native={}", name.display());
}
for name in &lib_names {
println!("cargo:rustc-link-lib=static={}", name);
}

// Ensure we trigger a rebuild if the libraries change, which is useful for
// development.
for lib_dir in &lib_dirs {
for name in &lib_names {
let path = lib_dir.join(format!("lib{}.a", name));
Expand All @@ -82,19 +124,36 @@ fn main() {
}
}

// System libraries
let system_libraries = [
"stdc++", // llvm-config --system-libs --link-static
"rt", "dl", "pthread", "m", "z", "tinfo",
];
for name in &system_libraries {
println!("cargo:rustc-link-lib=dylib={}", name);
// Get the library that must be linked for C++, if any.
// Soruce: https://gitlab.com/taricorp/llvm-sys.rs/-/blob/main/build.rs
let system_libcpp = if target_env_is("msvc") {
None
} else if target_os_is("macos") || target_os_is("freebsd") {
Some("c++")
} else {
Some("stdc++")
};
if let Some(system_libcpp) = system_libcpp {
println!("cargo:rustc-link-lib=dylib={}", system_libcpp);
}

// Link against system libraries needed for LLVM
for mut arg in llvm_config_run(llvm_config_bin, "--system-libs").split(&[' ', '\n']) {
if arg.is_empty() {
continue;
}
arg = arg.strip_prefix("-llib").unwrap_or(arg);
arg = arg.strip_prefix("-l").unwrap_or(arg);
arg = arg.strip_suffix(".tbd").unwrap_or(arg); // macos
arg = arg.strip_suffix(".lib").unwrap_or(arg); // msvc
println!("cargo:rustc-link-lib=dylib={}", arg);
}

// Make a list of include directories.
let include_dirs = vec![
circt_dir.join("include").display().to_string(),
circt_build_dir.join("include").display().to_string(),
llvm_include_dir,
llvm_dir.join("llvm/include").display().to_string(),
llvm_dir.join("mlir/include").display().to_string(),
llvm_build_dir.join("include").display().to_string(),
Expand Down
4 changes: 2 additions & 2 deletions src/circt-sys/wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ MlirAttribute mlirIntegerAttrGetFromString(MlirType type, MlirStringRef value) {
auto intWidth = intType.getWidth();
auto valueStr = unwrap(value);
auto tmpWidth = std::max<size_t>(intWidth, (valueStr.size() - 1) * 64 / 22);
return wrap(IntegerAttr::get(
intType, APInt(tmpWidth, valueStr, 10).truncOrSelf(intWidth)));
return wrap(
IntegerAttr::get(intType, APInt(tmpWidth, valueStr, 10).trunc(intWidth)));
}

bool mlirLocationIsFileLineCol(MlirLocation loc) {
Expand Down

0 comments on commit 789dbac

Please sign in to comment.