Giter Club home page Giter Club logo

Comments (4)

igaray avatar igaray commented on September 18, 2024 1

Good catch! want to send a PR correcting it or should we go ahead and fix it quick?

from cairo_native.

marcuspang avatar marcuspang commented on September 18, 2024 1

@azteca1998 thank you very much for the detailed response! I understand the flow much better now

from cairo_native.

marcuspang avatar marcuspang commented on September 18, 2024

@igaray actually, running this command by itself does not work out of the box. I had to write a wrapper C file to set it up. Is this intended?

For example, here is the command I ran for simple Cairo file compiled down to Sierra.

// program.cairo
fn main() -> u32 {
    println!("hello world");
    1
}
// wrapper.c
#include <stdint.h>

extern void alias_example_main(int64_t, __int128);

int main() {
    int64_t arg0 = 0;
    __int128 arg1 = 0;
    alias_example_main(arg0, arg1);
    return 0;
}
; program.ll

; added this line
@alias_example_main = alias { i64, i128, { i64, [24 x i8] } } (i64, i128), void (i64, i128)* @"example::example::main(f15270207278767002344)"

; main function definition
define { i64, i128, { i64, [24 x i8] } } @"example::example::main(f15270207278767002344)"(i64 %0, i128 %1) {
; ...
cairo-native-dump program.sierra -o program.mlir

mlir-opt \
    --canonicalize \
    --convert-scf-to-cf \
    --canonicalize \
    --cse \
    --expand-strided-metadata \
    --finalize-memref-to-llvm \
    --convert-func-to-llvm \
    --convert-index-to-llvm \
    --reconcile-unrealized-casts \
    "program.mlir" \
    -o "program-llvm.mlir"

mlir-translate --mlir-to-llvmir program-llvm.mlir -o program.ll

# only this one is different from README.md
clang wrapper.c program.ll -Wno-override-module \
    -L "$MLIR_SYS_180_PREFIX"/lib -L"./target/release/" \
    -lcairo_native_runtime -lmlir_c_runner_utils \
    -Wl,-rpath "$MLIR_SYS_180_PREFIX"/lib \
    -Wl,-rpath ./target/release/ \
    -o program

Even though I could compile the file, I still get a segfault 56807 segmentation fault ./program.

from cairo_native.

azteca1998 avatar azteca1998 commented on September 18, 2024

actually, running this command by itself does not work out of the box. I had to write a wrapper C file to set it up. Is this intended?
It's not whether it's intended or not, but rather:

  • Cairo always generates function names with their module, therefore it's impossible to have a main function.
  • Any non-trivial program will require gas and builtins, which do not exist in C, making the interface not work even if we solved the first problem.

As for your test program, try compiling it to Sierra and checking its function signature. In your case it's the following:

sample::sample::main@0([0]: RangeCheck, [1]: GasBuiltin) -> (RangeCheck, GasBuiltin, core::panics::PanicResult::<(core::integer::u32,)>);

As you can see the function needs RangeCheck and GasBuiltin, and returns (RangeCheck, GasBuiltin, core::panics::PanicResult::<(core::integer::u32,)>). You'd need something like this:

#include <stdio.h>
#include <stdint.h>


// Type consistency.
typedef __uint128_t uint128_t;


// Cairo Native types.
typedef struct __attribute__((aligned(16))) felt252_t
{
    uint64_t data[4];
} felt252_t;

typedef struct native_array_t
{
    void *data;
    uint32_t start_offset;
    uint32_t end_offset;
    uint32_t capacity;
} native_array_t;


// Our function's return values (check out the C ABI for more info).
typedef struct cairo_main_t
{
    uint64_t range_check;
    uint128_t gas;
    union {
        struct { uint8_t tag; uint32_t value; } ok;
        struct { uint8_t tag; native_array_t msg; } err;
    } result;
} cairo_main_t;


// The weakref is the symbol name. You should make sure it matches the one in the MLIR output,
// including the `_mlir_ciface_` prefix.
static cairo_main_t *cairo_main(cairo_main_t *, uint64_t, uint128_t)
    __attribute__((weakref("_mlir_ciface_program::program::main(f0)")));


int main()
{
    cairo_main_t ret_data;

    // You don't need to initialize the range check and gas into the return pointer, you can pass
    // them directly too. The final builtin counter and gas will be stored in the structure, so
    // having them originally there may make it easier to understand.
    ret_data.range_check = 0;
    ret_data.gas = 1000000000000;
    cairo_main(&ret_data, ret_data.range_check, ret_data.gas);

    // You can check either `ret_data.ok.tag` or `ret_data.err.tag`. They are the same field, but
    // must be declared separately in each variant due to the way our enums work (if it was a common
    // field, then the alignment of the payloads would be messed up).
    if (ret_data.result.ok.tag == 0)
        printf("cairo_main() returned Ok(%u)\n", ret_data.result.ok.value);
    else
    {
        felt252_t *error_msg = (felt252_t *) ret_data.result.err.msg.data;
        size_t i;

        // Here's an example on how to read the arrays (for the error messages):
        printf("cairo_main() returned an error:\n");
        for (i = ret_data.result.err.msg.start_offset; i < ret_data.result.err.msg.end_offset; i++)
        {
            // Note that they're in reverse order. It's intentional because they're written in
            // little-endian.
            printf(
                "  %016lx%016lx%016lx%016lx\n",
                error_msg[i].data[3],
                error_msg[i].data[2],
                error_msg[i].data[1],
                error_msg[i].data[0]
            );
        }
    }

    return  0;
}

Disclaimer: The C program hasn't been tested.

from cairo_native.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.