Giter Club home page Giter Club logo

btor2mlir's Introduction

Bᴛᴏʀ2ᴍʟɪʀ: A Format and Toolchain for Hardware Verification

os os Open In Colab

Results

Detailed analysis of run-times is available in an accompanying Jupyter Notebook in Google Collab. We also contribute translations of the 2019/20 Hardware Model Checking Competion benchmarks to our Btor Dialect, LLVM-IR and SMT-LIB in hwmcc20-mlir. The image below shows the different verification strategies employed.

strategies-img

Demo

Consider a simple counter that ensures we do not reach 15, represented in Bᴛᴏʀ2 below:

1 sort bitvec 4
2 zero 1
3 state 1 out
4 init 1 3 2
5 one 1
6 add 1 3 5
7 next 1 3 6
8 ones 1
9 sort bitvec 1
10 eq 9 3 8
11 bad 10

Using the command build/bin/btor2mlir-translate --import-btor counter.btor2 > counter.mlir, where counter.btor2 is the file shown above, we get the equivalent representation of our circuit in the BTOR Dialect of MLIR below (counter.mlir):

module {
  func @main() {
    %0 = btor.constant 0 : i4 !btor.bv<4>
    br ^bb1(%0 : !btor.bv<4>)
  ^bb1(%1: !btor.bv<4>):  // 2 preds: ^bb0, ^bb1
    %2 = btor.constant 1 : i4 !btor.bv<4>
    %3 = btor.add %1, %2 : !btor.bv<4>
    %4 = btor.constant -1 : i4 !btor.bv<4>
    %5 = btor.cmp eq, %1, %4 : !btor.bv<4>
    btor.assert_not(%5), 0 : i64 !btor.bv<1>
    br ^bb1(%3 : !btor.bv<4>)
  }
}

Then, using the command build/bin/btor2mlir-opt --convert-std-to-llvm --convert-btor-to-llvm counter.mlir > counter.mlir.opt we get the file below which represents the original circuit in the LLVM Dialect of MLIR.

module attributes {llvm.data_layout = ""} {
  llvm.func @__VERIFIER_error()
  llvm.func @__VERIFIER_assert(i1, i64)
  llvm.func @main() {
    %0 = llvm.mlir.constant(0 : i4) : i4
    llvm.br ^bb1(%0 : i4)
  ^bb1(%1: i4):  // 2 preds: ^bb0, ^bb2
    %2 = llvm.mlir.constant(1 : i4) : i4
    %3 = llvm.add %1, %2  : i4
    %4 = llvm.mlir.constant(-1 : i4) : i4
    %5 = llvm.icmp "eq" %1, %4 : i4
    %6 = llvm.mlir.constant(true) : i1
    %7 = llvm.xor %5, %6  : i1
    llvm.cond_br %7, ^bb2, ^bb3
  ^bb2:  // pred: ^bb1
    llvm.br ^bb1(%3 : i4)
  ^bb3:  // pred: ^bb1
    %8 = llvm.mlir.constant(0 : i64) : i64
    llvm.call @__VERIFIER_assert(%7, %8) : (i1, i64) -> ()
    llvm.call @__VERIFIER_error() : () -> ()
    llvm.unreachable
  }
}

Finally, using the command build/bin/btor2mlir-translate --mlir-to-llvmir counter.mlir.opt > counter.ll we generate the circuit as an LLVM-IR program below (counter.ll):

declare void @__VERIFIER_error()
declare void @__VERIFIER_assert(i1, i64)
define void @main() !dbg !3 {
  br label %1, !dbg !7
1:                                                ; preds = %6, %0
  %2 = phi i4 [ %3, %6 ], [ 0, %0 ]
  %3 = add i4 %2, 1, !dbg !9
  %4 = icmp eq i4 %2, -1, !dbg !10
  %5 = xor i1 %4, true, !dbg !11
  br i1 %5, label %6, label %7, !dbg !12
6:                                                ; preds = %1
  br label %1, !dbg !13
7:                                                ; preds = %1
  call void @__VERIFIER_assert(i1 %5, i64 0), !dbg !14
  call void @__VERIFIER_error(), !dbg !15
  unreachable, !dbg !16
}

If you have SeaHorn installed locally (a distribution is included in the Docker), we can show that the bad state in the original circuit is reached using SeaHorn's Bounded Model Checking engine. This is indicated by the output sat when we run the command: sea bpf counter.ll

Witness Generation

Run the shell script ./get_cex_seahorn.sh $btor2_file to:

a) extract a counter example from SeaHorn
b) generate a Btor2 Witness
c) simulate the witness using btorsim

Docker

Dockerfile: docker/btor2mlir.Dockerfile.

From the root folder use the following commands to:

Build: docker build -t btor2mlir . --file docker/btor2mlir.Dockerfile

Run: docker run -it btor2mlir

Building Locally

The instructions assume that cmake, clang/clang++ and ninja are installed on your machine, LLVM_PROJECT=/ag/llvm-gh-mlir, and that lit command is installed and is globally available

Building LLVM

Commands to configure and compile LLVM

$ mkdir debug && cd debug 
$ cmake -G Ninja ../llvm \
    -DCMAKE_C_COMPILER=clang-14 -DCMAKE_CXX_COMPILER=clang++-14 \
    -DLLVM_ENABLE_PROJECTS=mlir -DLLVM_BUILD_EXAMPLES=ON  \ 
    -DCMAKE_BUILD_TYPE=Debug \ # change to RelWithDebInfo for release build
    -DLLVM_TARGETS_TO_BUILD="X86"  \
    -DLLVM_ENABLE_LLD=ON  \ # only on Linux	
    -DLLVM_INSTALL_UTILS=ON \ # optional to install FileCheck and lit
    -DCMAKE_INSTALL_PREFIX=$(pwd)/run  # install location in `run` under build
$ ninja
$ ninja install

The above installs a debug version of llvm under LLVM_PROJECT/debug/run, where LLVM_PROJECT is the root of llvm project on your machine.

Building

To compile this project

$ mkdir debug && cd debug 
$ cmake -G Ninja .. \
    -DMLIR_DIR=/ag/llvm-gh-mlir/debug/run/lib/cmake/mlir \
    -DLLVM_DIR=/ag/llvm-gh-mlir/debug/run/lib/cmake/llvm \
    -DLLVM_EXTERNAL_LIT=$(which lit) \
    -DLLVM_ENABLE_LLD=ON \
    -DCMAKE_INSTALL_PREFIX=$(pwd)/run \

Contributors

Arie Gurfinkel [email protected]
Joseph Tafese [email protected]

btor2mlir's People

Contributors

agurfinkel avatar hennyg888 avatar jetafese avatar jitao-hu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

btor2mlir's Issues

Btor2-to-MLIR translation failed due to segmentation fault

I ran the below command (following the instructions in README)
to translate the Btor2 circuit array_swap.btor2 (obtained from HWMCC 2020 benchmark set) to the Btor2 dialect of MLIR.
A docker image built from commit 6feb6ad was used.

build/bin/btor2mlir-translate --import-btor array_swap.btor2 > array_swap.mlir

The command failed with the error message:

PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.	Program arguments: build/bin/btor2mlir-translate --import-btor array_swap.btor2
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
/usr/lib/llvm-14/lib/libLLVM-14.so.1(_ZN4llvm3sys15PrintStackTraceERNS_11raw_ostreamEi+0x31)[0x7f56bae8cd01]
/usr/lib/llvm-14/lib/libLLVM-14.so.1(_ZN4llvm3sys17RunSignalHandlersEv+0xee)[0x7f56bae8aa3e]
/usr/lib/llvm-14/lib/libLLVM-14.so.1(+0xe40236)[0x7f56bae8d236]
/lib/x86_64-linux-gnu/libc.so.6(+0x42520)[0x7f56b9b30520]
build/bin/btor2mlir-translate(+0x19fce0)[0x559794aface0]
build/bin/btor2mlir-translate(+0x19fcb9)[0x559794afacb9]
build/bin/btor2mlir-translate(+0x1961d8)[0x559794af11d8]
build/bin/btor2mlir-translate(+0x194568)[0x559794aef568]
build/bin/btor2mlir-translate(+0x192036)[0x559794aed036]
build/bin/btor2mlir-translate(+0x195010)[0x559794af0010]
build/bin/btor2mlir-translate(+0x1958ba)[0x559794af08ba]
build/bin/btor2mlir-translate(+0x196898)[0x559794af1898]
build/bin/btor2mlir-translate(+0x1b7db5)[0x559794b12db5]
build/bin/btor2mlir-translate(+0x1b7c36)[0x559794b12c36]
build/bin/btor2mlir-translate(+0x1b7b6a)[0x559794b12b6a]
build/bin/btor2mlir-translate(+0x1b7aaa)[0x559794b12aaa]
build/bin/btor2mlir-translate(+0x1b796a)[0x559794b1296a]
build/bin/btor2mlir-translate(+0x5ce15a)[0x559794f2915a]
build/bin/btor2mlir-translate(+0x5cddeb)[0x559794f28deb]
build/bin/btor2mlir-translate(+0x5ccfcc)[0x559794f27fcc]
build/bin/btor2mlir-translate(+0x190487)[0x559794aeb487]
/lib/x86_64-linux-gnu/libc.so.6(+0x29d90)[0x7f56b9b17d90]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0x80)[0x7f56b9b17e40]
build/bin/btor2mlir-translate(+0x190365)[0x559794aeb365]
Segmentation fault (core dumped)

As I am not very familiar with MLIR and LLVM,
I am a bit uncertain whether the segmentation fault is caused
by Btor2MLIR or the MLIR/LLVM infrastructure.
If it is the latter case, feel free to close the issue.

MLIR-to-LLVM-IR translation failed because 'btor.nd_state' is unknown

I ran the below commands (following the instructions in README)
to translate the Btor2 circuit noninitstate.btor2 to an LLVM-IR program.
A docker image built from commit 6feb6ad was used.

  1. build/bin/btor2mlir-translate --import-btor noninitstate.btor2 > noninitstate.mlir
  2. build/bin/btor2mlir-opt --convert-std-to-llvm --convert-btor-to-llvm noninitstate.mlir > noninitstate.mlir.opt
  3. build/bin/btor2mlir-translate --mlir-to-llvmir noninitstate.mlir.opt > noninitstate.ll

The first 2 steps succeeded, but the 3rd one failed with the error message:

noninitstate.mlir.opt:6:10: error: custom op 'btor.nd_state' is unknown
    %0 = btor.nd_state 0 : !btor.bv<1>
         ^

Should I specify any additional command-line arguments to btor2mlir-translate or btor2mlir-opt to make the translation work?
Or the translation of non-initialized states in Btor2 to LLVM-IR is currently not supported?

MLIR-to-LLVM-IR translation failed due to unregistered dialect

I ran the below commands (following the instructions in README)
to translate the Btor2 circuit simple-stack-pred1.btor2 (obtained from HWMCC 2020 benchmark set) to an LLVM-IR program.
A docker image built from commit 6feb6ad was used.

  1. build/bin/btor2mlir-translate --import-btor simple-stack-pred1.btor2 > simple-stack-pred1.mlir
  2. build/bin/btor2mlir-opt --convert-std-to-llvm --convert-btor-to-llvm simple-stack-pred1.mlir > simple-stack-pred1.mlir.opt
  3. build/bin/btor2mlir-translate --mlir-to-llvmir simple-stack-pred1.mlir.opt > simple-stack-pred1.ll

The first 2 steps succeeded, but the 3rd one failed with the error message:

simple-stack-pred1.mlir.opt:9:63: error: `!"btor"<"bv<32>">` type created with unregistered dialect. If this is intended, please call allowUnregisteredDialects() on the MLIRContext, or use -allow-unregistered-dialect with the MLIR opt tool used
    %3 = builtin.unrealized_conversion_cast %2 : i32 to !btor.bv<32>
                                                              ^

I added -allow-unregistered-dialect to the 2nd command as the error message suggested,
but the same error persisted.

Should I specify any additional command-line arguments to btor2mlir-translate or btor2mlir-opt to make the translation work?
Or are there any specific Btor2 features in this example that are currently not supported?

MLIR-to-LLVM-IR translation failed because 'btor.input' is unknown

I ran the below commands (following the instructions in README)
to translate the Btor2 circuit recount4.btor2 to an LLVM-IR program.
A docker image built from commit 6feb6ad was used.

  1. build/bin/btor2mlir-translate --import-btor recount4.btor2 > recount4.mlir
  2. build/bin/btor2mlir-opt --convert-std-to-llvm --convert-btor-to-llvm recount4.mlir > recount4.mlir.opt
  3. build/bin/btor2mlir-translate --mlir-to-llvmir recount4.mlir.opt > recount4.ll

The first 2 steps succeeded, but the 3rd one failed with the error message:

recount4.mlir.opt:10:10: error: custom op 'btor.input' is unknown
    %4 = btor.input 0 : !btor.bv<1>
         ^

Should I specify any additional command-line arguments to btor2mlir-translate or btor2mlir-opt to make the translation work?
Or the translation of Btor2's input to LLVM-IR is currently not supported?

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.