1
0
forked from M-Labs/nac3

Compare commits

...

1 Commits

Author SHA1 Message Date
00112562f5 wip: Remove params from exceptions and replace message to static buffer
Still messed the message IDs somewhere

Signed-off-by: Egor Savkin <es@m-labs.hk>
2025-02-19 11:13:17 +08:00
12 changed files with 37 additions and 113 deletions

6
flake.lock generated
View File

@ -2,11 +2,11 @@
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1738680400,
"narHash": "sha256-ooLh+XW8jfa+91F1nhf9OF7qhuA/y1ChLx6lXDNeY5U=",
"lastModified": 1739736696,
"narHash": "sha256-zON2GNBkzsIyALlOCFiEBcIjI4w38GYOb+P+R4S8Jsw=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "799ba5bffed04ced7067a91798353d360788b30d",
"rev": "d74a2335ac9c133d6bbec9fc98d91a77f1604c1f",
"type": "github"
},
"original": {

View File

@ -9,7 +9,7 @@
pkgs32 = import nixpkgs { system = "i686-linux"; };
in rec {
packages.x86_64-linux = rec {
llvm-nac3 = pkgs.callPackage ./nix/llvm {};
llvm-nac3 = pkgs.callPackage ./nix/llvm { debugVersion = true; };
llvm-tools-irrt = pkgs.runCommandNoCC "llvm-tools-irrt" {}
''
mkdir -p $out/bin
@ -22,6 +22,7 @@
cargoLock = {
lockFile = ./nac3standalone/demo/linalg/Cargo.lock;
};
buildType = "debug";
doCheck = false;
};
demo-linalg-stub32 = pkgs32.rustPlatform.buildRustPackage {
@ -30,6 +31,7 @@
cargoLock = {
lockFile = ./nac3standalone/demo/linalg/Cargo.lock;
};
buildType = "debug";
doCheck = false;
};
nac3artiq = pkgs.python3Packages.toPythonModule (
@ -44,6 +46,8 @@
nativeBuildInputs = [ pkgs.python3 (pkgs.wrapClangMulti pkgs.llvmPackages_14.clang) llvm-tools-irrt pkgs.llvmPackages_14.llvm.out pkgs.llvmPackages_14.bintools llvm-nac3 ];
buildInputs = [ pkgs.python3 llvm-nac3 ];
checkInputs = [ (pkgs.python3.withPackages(ps: [ ps.numpy ps.scipy ])) ];
doCheck = false;
buildType = "debug";
checkPhase =
''
echo "Checking nac3standalone demos..."
@ -60,13 +64,13 @@
''
PYTHON_SITEPACKAGES=$out/${pkgs.python3Packages.python.sitePackages}
mkdir -p $PYTHON_SITEPACKAGES
cp target/x86_64-unknown-linux-gnu/release/libnac3artiq.so $PYTHON_SITEPACKAGES/nac3artiq.so
cp target/x86_64-unknown-linux-gnu/debug/libnac3artiq.so $PYTHON_SITEPACKAGES/nac3artiq.so
mkdir -p $runkernel/bin
cp target/x86_64-unknown-linux-gnu/release/runkernel $runkernel/bin
cp target/x86_64-unknown-linux-gnu/debug/runkernel $runkernel/bin
mkdir -p $standalone/bin
cp target/x86_64-unknown-linux-gnu/release/nac3standalone $standalone/bin
cp target/x86_64-unknown-linux-gnu/debug/nac3standalone $standalone/bin
'';
}
);
@ -89,6 +93,7 @@
buildInputs = [ pkgs.python3 llvm-nac3-instrumented ];
cargoBuildFlags = [ "--package" "nac3artiq" "--features" "init-llvm-profile" ];
doCheck = false;
buildType = "debug";
configurePhase =
''
export CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUSTFLAGS="-C link-arg=-L${pkgs.llvmPackages_14.compiler-rt}/lib/linux -C link-arg=-lclang_rt.profile-x86_64"
@ -97,7 +102,7 @@
''
TARGET_DIR=$out/${pkgs.python3Packages.python.sitePackages}
mkdir -p $TARGET_DIR
cp target/x86_64-unknown-linux-gnu/release/libnac3artiq.so $TARGET_DIR/nac3artiq.so
cp target/x86_64-unknown-linux-gnu/debug/libnac3artiq.so $TARGET_DIR/nac3artiq.so
'';
}
);
@ -131,7 +136,7 @@
export PYTHONPATH=$sipyco:$artiq
python -m artiq.frontend.artiq_ddb_template $artiq/artiq/examples/nac3devices/nac3devices.json > device_db.py
cp $artiq/artiq/examples/nac3devices/nac3devices.py .
python -m artiq.frontend.artiq_compile nac3devices.py
#python -m artiq.frontend.artiq_compile nac3devices.py
'';
installPhase =
''
@ -147,6 +152,7 @@
pkgs.rustPlatform.buildRustPackage {
name = "nac3artiq-pgo";
src = self;
buildType = "debug";
inherit (nac3artiq) cargoLock;
nativeBuildInputs = [ pkgs.python3 packages.x86_64-linux.llvm-tools-irrt pkgs.llvmPackages_14.bintools llvm-nac3-pgo ];
buildInputs = [ pkgs.python3 llvm-nac3-pgo ];
@ -156,7 +162,7 @@
''
TARGET_DIR=$out/${pkgs.python3Packages.python.sitePackages}
mkdir -p $TARGET_DIR
cp target/x86_64-unknown-linux-gnu/release/libnac3artiq.so $TARGET_DIR/nac3artiq.so
cp target/x86_64-unknown-linux-gnu/debug/libnac3artiq.so $TARGET_DIR/nac3artiq.so
'';
}
);

View File

@ -1467,30 +1467,9 @@ fn polymorphic_print<'ctx>(
None,
)
.into_int_value();
let param0 = ctx
.build_in_bounds_gep_and_load(
exn,
&[llvm_i32.const_zero(), llvm_i32.const_int(6, false)],
None,
)
.into_int_value();
let param1 = ctx
.build_in_bounds_gep_and_load(
exn,
&[llvm_i32.const_zero(), llvm_i32.const_int(7, false)],
None,
)
.into_int_value();
let param2 = ctx
.build_in_bounds_gep_and_load(
exn,
&[llvm_i32.const_zero(), llvm_i32.const_int(8, false)],
None,
)
.into_int_value();
fmt.push_str(fmt_str.as_str());
args.extend_from_slice(&[name.into(), param0.into(), param1.into(), param2.into()]);
args.extend_from_slice(&[name.into(),]);
}
_ => unreachable!(

View File

@ -1177,6 +1177,7 @@ impl Nac3 {
};
let id = i32::try_from(i).unwrap();
println!("Nac3::new: {} -> {}", &exn_name, &id);
string_store.insert(exn_name, id);
}

View File

@ -1769,6 +1769,7 @@ impl SymbolResolver for Resolver {
*id
} else {
let id = i32::try_from(string_store.len()).unwrap();
println!("get_string_id(&self, s: &str): {} -> {}", s, id);
string_store.insert(s.into(), id);
id
}

View File

@ -28,6 +28,13 @@ ExceptionId EXN_TYPE_ERROR;
extern "C" void __nac3_raise(void* err);
namespace {
template<typename SizeT>
struct StringBuffer {
SizeT pos;
unsigned char buf[128];
};
/**
* @brief NAC3's Exception struct
*/
@ -38,8 +45,7 @@ struct Exception {
int32_t line;
int32_t column;
CSlice<SizeT> function;
CSlice<SizeT> msg;
int64_t params[3];
StringBuffer<SizeT> msg;
};
constexpr int64_t NO_PARAM = 0;
@ -49,10 +55,7 @@ void _raise_exception_helper(ExceptionId id,
const char* filename,
int32_t line,
const char* function,
const char* msg,
int64_t param0,
int64_t param1,
int64_t param2) {
const char* msg) {
Exception<SizeT> e = {
.id = id,
.filename = {.base = reinterpret_cast<void*>(const_cast<char*>(filename)),
@ -61,12 +64,11 @@ void _raise_exception_helper(ExceptionId id,
.column = 0,
.function = {.base = reinterpret_cast<void*>(const_cast<char*>(function)),
.len = static_cast<SizeT>(__builtin_strlen(function))},
.msg = {.base = reinterpret_cast<void*>(const_cast<char*>(msg)),
.len = static_cast<SizeT>(__builtin_strlen(msg))},
.msg = {},
};
e.params[0] = param0;
e.params[1] = param1;
e.params[2] = param2;
e.msg.pos = __builtin_strlen(msg);
__builtin_memcpy(&e.msg.buf, msg, e.msg.pos);
__nac3_raise(reinterpret_cast<void*>(&e));
__builtin_unreachable();
}
@ -82,4 +84,4 @@ void _raise_exception_helper(ExceptionId id,
* `NO_PARAM` to indicate they are unused.
*/
#define raise_exception(SizeT, id, msg, param0, param1, param2) \
_raise_exception_helper<SizeT>(id, __FILE__, __LINE__, __FUNCTION__, msg, param0, param1, param2)
_raise_exception_helper<SizeT>(id, __FILE__, __LINE__, __FUNCTION__, msg)

View File

@ -639,20 +639,7 @@ impl<'ctx> CodeGenContext<'ctx, '_> {
.unwrap();
self.builder.build_store(ptr, msg).unwrap();
let i64_zero = self.ctx.i64_type().const_zero();
for (i, attr_ind) in [6, 7, 8].iter().enumerate() {
let ptr = self
.builder
.build_in_bounds_gep(
zelf,
&[zero, int32.const_int(*attr_ind, false)],
"exn.param",
)
.unwrap();
let val = params[i].map_or(i64_zero, |v| {
self.builder.build_int_s_extend(v, self.ctx.i64_type(), "sext").unwrap()
});
self.builder.build_store(ptr, val).unwrap();
}
}
gen_raise(generator, self, Some(&zelf.into()), loc);
}

View File

@ -1298,18 +1298,7 @@ pub fn exn_constructor<'ctx>(
args.remove(0).1.to_basic_value_enum(ctx, generator, ctx.primitives.str)?
};
ctx.builder.build_store(ptr, msg).unwrap();
for i in &[6, 7, 8] {
let value = if args.is_empty() {
ctx.ctx.i64_type().const_zero().into()
} else {
args.remove(0).1.to_basic_value_enum(ctx, generator, ctx.primitives.int64)?
};
let ptr = ctx
.builder
.build_in_bounds_gep(zelf, &[zero, int32.const_int(*i, false)], "exn.param")
.unwrap();
ctx.builder.build_store(ptr, value).unwrap();
}
// set file, func to empty string
for i in &[1, 4] {
let ptr = ctx

View File

@ -46,25 +46,7 @@ pub fn get_exn_constructor(
ty: string,
default_value: Some(SymbolValue::Str(String::new())),
is_vararg: false,
},
FuncArg {
name: "param0".into(),
ty: int64,
default_value: Some(SymbolValue::I64(0)),
is_vararg: false,
},
FuncArg {
name: "param1".into(),
ty: int64,
default_value: Some(SymbolValue::I64(0)),
is_vararg: false,
},
FuncArg {
name: "param2".into(),
ty: int64,
default_value: Some(SymbolValue::I64(0)),
is_vararg: false,
},
}
];
let exn_type = unifier.add_ty(TypeEnum::TObj {
obj_id: DefinitionId(class_id),

View File

@ -1530,25 +1530,7 @@ impl TopLevelComposer {
ty: string,
default_value: Some(SymbolValue::Str(String::new())),
is_vararg: false,
},
FuncArg {
name: "param0".into(),
ty: int64,
default_value: Some(SymbolValue::I64(0)),
is_vararg: false,
},
FuncArg {
name: "param1".into(),
ty: int64,
default_value: Some(SymbolValue::I64(0)),
is_vararg: false,
},
FuncArg {
name: "param2".into(),
ty: int64,
default_value: Some(SymbolValue::I64(0)),
is_vararg: false,
},
}
],
ret: self_type,
vars: VarMap::default(),

View File

@ -370,9 +370,6 @@ pub fn make_exception_fields(int32: Type, int64: Type, str: Type) -> Vec<(StrRef
("__col__".into(), int32, true),
("__func__".into(), str, true),
("__message__".into(), str, true),
("__param0__".into(), int64, true),
("__param1__".into(), int64, true),
("__param2__".into(), int64, true),
]
}

View File

@ -114,7 +114,6 @@ struct Exception {
uint32_t column;
struct cslice function;
struct cslice message;
int64_t param[3];
};
uint32_t __nac3_raise(struct Exception* e) {
@ -124,7 +123,6 @@ uint32_t __nac3_raise(struct Exception* e) {
e->column);
printf(" Function: %*s\n", (int)e->function.len, (const char*)e->function.data);
printf(" Message: \"%*s\"\n", (int)e->message.len, (const char*)e->message.data);
printf(" Params: {0}=%" PRId64 ", {1}=%" PRId64 ", {2}=%" PRId64 "\n", e->param[0], e->param[1], e->param[2]);
exit(101);
__builtin_unreachable();
}