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

Add debug locations #512

Merged
merged 7 commits into from
Apr 16, 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
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ walkdir = "2"
serde_json = { version = "1.0" }

[build-dependencies]
cc = "1.0.90"
cc = "1.0.92"

[profile.optimized-dev]
inherits = "dev"
Expand Down
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -801,3 +801,41 @@ cairo-native-test ./cairo-tests/
```

This will run all the tests (functions marked with the `#[test]` attribute).

## Debugging Tips

### Useful environment variables

These 2 env vars will dump the generated MLIR code from any compilation on the current working directory as:

- `dump.mlir`: The MLIR code after passes without locations.
- `dump-debug.mlir`: The MLIR code after passes with locations.
- `dump-prepass.mlir`: The MLIR code before without locations.
- `dump-prepass-debug.mlir`: The MLIR code before passes with locations.

Do note that the MLIR with locations is in pretty form and thus not suitable to pass to `mlir-opt`.

```bash
export NATIVE_DEBUG_DUMP_PREPASS=1
export NATIVE_DEBUG_DUMP=1
```

Enable logging to see the compilation process:

```bash
export RUST_LOG="cairo_native=trace"
```

Other tips:

- Try to find the minimal program to reproduce an issue, the more isolated the easier to test.
- Use the `debug_utils` print utilities, more info [here](https://lambdaclass.github.io/cairo_native/cairo_native/metadata/debug_utils/struct.DebugUtils.html):

```rust
#[cfg(feature = "with-debug-utils")]
{
metadata.get_mut::<DebugUtils>()
.unwrap()
.print_pointer(context, helper, entry, ptr, location)?;
}
```
8 changes: 4 additions & 4 deletions benches/compile_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn bench_compile_time(c: &mut Criterion) {
c.bench_with_input(BenchmarkId::new(filename, 1), &program, |b, program| {
b.iter(|| {
let native_context = NativeContext::new();
native_context.compile(program).unwrap();
native_context.compile(program, None).unwrap();
// pass manager internally verifies the MLIR output is correct.
})
});
Expand All @@ -29,7 +29,7 @@ pub fn bench_compile_time(c: &mut Criterion) {
for (program, filename) in &programs {
c.bench_with_input(BenchmarkId::new(filename, 1), &program, |b, program| {
b.iter(|| {
native_context.compile(program).unwrap();
native_context.compile(program, None).unwrap();
// pass manager internally verifies the MLIR output is correct.
})
});
Expand All @@ -43,7 +43,7 @@ pub fn bench_compile_time(c: &mut Criterion) {
c.bench_with_input(BenchmarkId::new(filename, 1), &program, |b, program| {
b.iter(|| {
let native_context = NativeContext::new();
let module = native_context.compile(black_box(program)).unwrap();
let module = native_context.compile(black_box(program), None).unwrap();
let object = module_to_object(module.module(), OptLevel::None)
.expect("to compile correctly to a object file");
black_box(object)
Expand All @@ -60,7 +60,7 @@ pub fn bench_compile_time(c: &mut Criterion) {
for (program, filename) in &programs {
c.bench_with_input(BenchmarkId::new(filename, 1), &program, |b, program| {
b.iter(|| {
let module = native_context.compile(black_box(program)).unwrap();
let module = native_context.compile(black_box(program), None).unwrap();
let object = module_to_object(module.module(), OptLevel::None)
.expect("to compile correctly to a object file");
black_box(object)
Expand Down
4 changes: 2 additions & 2 deletions benches/libfuncs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub fn bench_libfuncs(c: &mut Criterion) {
|b, program| {
let native_context = NativeContext::new();
b.iter(|| {
let module = native_context.compile(program).unwrap();
let module = native_context.compile(program, None).unwrap();
// pass manager internally verifies the MLIR output is correct.
let native_executor =
JitNativeExecutor::from_native_module(module, Default::default());
Expand All @@ -69,7 +69,7 @@ pub fn bench_libfuncs(c: &mut Criterion) {
program,
|b, program| {
let native_context = NativeContext::new();
let module = native_context.compile(program).unwrap();
let module = native_context.compile(program, None).unwrap();
// pass manager internally verifies the MLIR output is correct.
let native_executor =
JitNativeExecutor::from_native_module(module, Default::default());
Expand Down
13 changes: 10 additions & 3 deletions examples/easy_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,22 @@ use std::path::Path;

fn main() {
let program_path = Path::new("programs/examples/hello.cairo");
// Compile the cairo program to sierra.
let sierra_program = cairo_native::utils::cairo_to_sierra(program_path);

// Instantiate a Cairo Native MLIR context. This data structure is responsible for the MLIR
// initialization and compilation of sierra programs into a MLIR module.
let native_context = NativeContext::new();

// Compile the cairo program to sierra.
let (sierra_program, debug_locations) = cairo_native::utils::cairo_to_sierra_with_debug_info(
native_context.context(),
program_path,
)
.unwrap();

// Compile the sierra program into a MLIR module.
let native_program = native_context.compile(&sierra_program).unwrap();
let native_program = native_context
.compile(&sierra_program, Some(debug_locations))
.unwrap();

// The parameters of the entry point.
let params = &[JitValue::Felt252(Felt::from_bytes_be_slice(b"user"))];
Expand Down
2 changes: 1 addition & 1 deletion examples/erc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ fn main() {

let native_context = NativeContext::new();

let native_program = native_context.compile(&sierra_program).unwrap();
let native_program = native_context.compile(&sierra_program, None).unwrap();

let entry_point_fn =
find_entry_point_by_idx(&sierra_program, entry_point.function_idx).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion examples/invoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn main() {

let native_context = NativeContext::new();

let native_program = native_context.compile(&sierra_program).unwrap();
let native_program = native_context.compile(&sierra_program, None).unwrap();

// Call the echo function from the contract using the generated wrapper.

Expand Down
2 changes: 1 addition & 1 deletion examples/starknet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ fn main() {

let native_context = NativeContext::new();

let native_program = native_context.compile(&sierra_program).unwrap();
let native_program = native_context.compile(&sierra_program, None).unwrap();

// Call the echo function from the contract using the generated wrapper.

Expand Down
19 changes: 9 additions & 10 deletions src/bin/cairo-native-compile.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use anyhow::Context;
use cairo_lang_compiler::{compile_cairo_project_at_path, CompilerConfig};
use cairo_native::{context::NativeContext, module_to_object, object_to_shared_lib, OptLevel};
use cairo_native::{
context::NativeContext, module_to_object, object_to_shared_lib,
utils::cairo_to_sierra_with_debug_info, OptLevel,
};
use clap::{Parser, ValueEnum};
use std::path::{Path, PathBuf};
use tracing_subscriber::{EnvFilter, FmtSubscriber};
Expand Down Expand Up @@ -49,17 +51,14 @@ fn main() -> anyhow::Result<()> {
// Check if args.path is a file or a directory.
check_compiler_path(args.single_file, &args.path)?;

let sierra_program = compile_cairo_project_at_path(
&args.path,
CompilerConfig {
replace_ids: args.replace_ids,
..CompilerConfig::default()
},
)?;
let native_context = NativeContext::new();
let (sierra_program, debug_locations) =
cairo_to_sierra_with_debug_info(native_context.context(), &args.path)?;

// Compile the sierra program into a MLIR module.
let native_module = native_context.compile(&sierra_program).unwrap();
let native_module = native_context
.compile(&sierra_program, Some(debug_locations))
.unwrap();

let opt_level = match args.opt_level {
0 => OptLevel::None,
Expand Down
4 changes: 2 additions & 2 deletions src/bin/cairo-native-dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// Load the program.
let context = NativeContext::new();
// TODO: Reconnect debug information.
let (program, _debug_info) = load_program(
let (program, debug_info) = load_program(
Path::new(&args.input),
Some(context.context()),
args.starknet,
)?;

// Compile the program.
let module = context.compile(&program)?;
let module = context.compile(&program, debug_info)?;

// Write the output.
let output_str = module
Expand Down
17 changes: 16 additions & 1 deletion src/bin/cairo-native-run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use cairo_lang_sierra_generator::{
use cairo_lang_starknet::contract::get_contracts_info;
use cairo_native::{
context::NativeContext,
debug_info::{DebugInfo, DebugLocations},
execution_result::ExecutionResult,
executor::{AotNativeExecutor, JitNativeExecutor, NativeExecutor},
metadata::gas::{GasMetadata, MetadataComputationConfig},
Expand Down Expand Up @@ -93,8 +94,22 @@ fn main() -> anyhow::Result<()> {

let native_context = NativeContext::new();

let debug_locations = {
let debug_info = DebugInfo::extract(db, &sierra_program)
.map_err(|_| {
let mut buffer = String::new();
assert!(DiagnosticsReporter::write_to_string(&mut buffer).check(db));
buffer
})
.unwrap();

DebugLocations::extract(native_context.context(), db, &debug_info)
};

// Compile the sierra program into a MLIR module.
let native_module = native_context.compile(&sierra_program).unwrap();
let native_module = native_context
.compile(&sierra_program, Some(debug_locations))
.unwrap();

let opt_level = match args.opt_level {
0 => OptLevel::None,
Expand Down
2 changes: 1 addition & 1 deletion src/cache/aot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ where
module,
registry,
metadata,
} = self.context.compile(program).expect("should compile");
} = self.context.compile(program, None).expect("should compile");

// Compile module into an object.
let object_data = crate::ffi::module_to_object(&module, opt_level).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/cache/jit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ where
program: &Program,
opt_level: OptLevel,
) -> Rc<JitNativeExecutor<'a>> {
let module = self.context.compile(program).expect("should compile");
let module = self.context.compile(program, None).expect("should compile");
let executor = JitNativeExecutor::from_native_module(module, opt_level);

let executor = Rc::new(executor);
Expand Down
Loading
Loading