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

trying reuse filetests #211

Merged
merged 11 commits into from
Feb 22, 2024
Merged

trying reuse filetests #211

merged 11 commits into from
Feb 22, 2024

Conversation

MCJOHN974
Copy link

Just small attempt to reuse some infra from filetests, for example .clif runtests parsing. Not ready to use

@MCJOHN974
Copy link
Author

MCJOHN974 commented Feb 12, 2024

This PR aims only integrate some code to filetest infrastructure, reuse .clif files parsing, and just to compile some clif to zkasm and print it to stdout. No new checks performed.

It would be followed up by next set of PRs:

  1. adding assert as host function to zkasm, such that assert fail don't halt the execution.
  2. assembling generated pieces of zkasm to one program
  3. final assembling of all previous work together & migrating CI from old version to new version.

@MCJOHN974 MCJOHN974 marked this pull request as ready for review February 12, 2024 13:27
@MCJOHN974
Copy link
Author

@Akashin, @mooori it's ready for review now.

cranelift/filetests/filetests/runtests/arithmetic.clif Outdated Show resolved Hide resolved

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use existing arithmetic.clif or is there some reason it wouldn't work?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

arithmetic.clif panics for now, and changes to fix it worth separate PR

cranelift/filetests/src/test_run_zkasm.rs Outdated Show resolved Hide resolved

// just wrap to make CI green until executor is not ready
// TODO: replace by actual execution and results checking
let _ = panic::catch_unwind(AssertUnwindSafe(|| {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use panic::catch_unwind rather sparingly as it increases the chance of catching and ignoring the wrong types of errors. Instead, we should rely on std::result and check that we get the appropriate error code.

In this case, though, I think we can simplify things by removing the todo! statement from the end of execute and then we don't need the catch_unwind at all.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From once side yes, it will simplify, from the other side, this annoying catch_unwind and todo! explicitly marks that this parts are not ready and should be rewritten

Ok(())
}

fn generate_zkasm(_func_store: &FunctionStore, func: &Function) -> Vec<String> {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not great that we duplicate the code from https://github.com/near/wasmtime/blob/main/cranelift/filetests/src/test_zkasm.rs. We need to move the shared bits into a separate module.
For example, we can start by creating a module in cranelift/filetests/src/zkasm_codegen.rs and moving the necessary functions from https://github.com/near/wasmtime/blob/main/cranelift/filetests/src/test_zkasm.rs for this PR. Later, we can move all relevant functions.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we will remove test_zkasm.rs once test_run_zkasm.rs will be ready. And than do some refactoring, such as creating zkasm_codegen.rs if it will be needed.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that code duplication like this should be avoided. This code might be touched for upcoming refactorings or zkASM instrumentation. Keeping duplicate code in sync is tedious and if it gets out of sync that might lead to issues which are hard to debug.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm definitely agree that we don't want have a code duplication. But, one more problem, it's not pure duplication, this functions are rewrited a bit for test_run_zkasm.rs. And, until filetest infra is not ready we want to keep both versions -- one to keep existing test infra alive, the second one -- to make iterative progress to the new test infra.

As alternative, I can suggest to create some filtest-dev branch. Code still will be reviewed before go to that branch, but we can make some exceptions for such problems

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hopefully, #216 can be a good start for the refactoring to avoid duplication.

But, one more problem, it's not pure duplication, this functions are rewrited a bit for test_run_zkasm.rs

Can you please document the necessary changes so that it is easier to reconcile these two functions in the future?

@MCJOHN974
Copy link
Author

Btw does anybody knows what this failed windows tests telling? I see cranelift\filetests\src\zkasm_runner.rs:117:39: but we even don't have such file... Is this file generated during compilation?..

@aborg-dev
Copy link

Btw does anybody knows what this failed windows tests telling? I see cranelift\filetests\src\zkasm_runner.rs:117:39: but we even don't have such file... Is this file generated during compilation?..

Btw does anybody knows what this failed windows tests telling? I see cranelift\filetests\src\zkasm_runner.rs:117:39: but we even don't have such file... Is this file generated during compilation?..

This file was added in #213 - have you rebased on the latest main branch?

@MCJOHN974
Copy link
Author

Last commit changed structure of test_run_zkasm.rs a lot, and now it builds some .zkasm program from whole test file. It is still WIP, and I left a lot of TODOs, but I think it can be merged as iteration step

@MCJOHN974 MCJOHN974 requested a review from aborg-dev February 14, 2024 00:31
_: &'a Flags,
_: Option<&'a dyn TargetIsa>,
) -> anyhow::Result<()> {
let mut zkasm_functions: Vec<Vec<String>> = Default::default();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Given that we have a simple Vec type on the left, we could use a more specific Vec::new() instead of Default::default():

  • This will avoid bringing in additional dependency on Default
  • The resulting code will be simpler (shorter; don't have to know what is the Default::default() for Vec)

let mut zkasm_functions: Vec<Vec<String>> = Default::default();
let mut invocations: Vec<Vec<String>> = Default::default();
for (func, details) in &testfile.functions {
let zkasm_function = zkasm_codegen::compile_clif_function(func);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I don't think zkasm_function temporary variable adds any more clarity to the code, consider inlining it:

zkasm_functions.push(zkasm_codegen::compile_clif_function(func));

RunCommand::Print(_) => {
unreachable!()
}
RunCommand::Run(invoce, compare, expected) => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Counterintuitive, but a verb from "invocation" is "invoke".

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hehe, I was thinking for a moment that "invoke" looks much better, thanks!

unreachable!()
}
RunCommand::Run(invoce, compare, expected) => {
let invocation =

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto: this temporary is not adding much value

let isa = isa_builder
.finish(settings::Flags::new(flag_builder))
.unwrap();
let mut comp_ctx = cranelift_codegen::Context::for_function(func.clone());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe "comp_ctx" -> "context" to follow the same convention as generate_zkasm above?

// now it works for one very basic case
pub fn build_test_zkasm(functions: Vec<Vec<String>>, invocations: Vec<Vec<String>>) -> String {
// TODO: use generate_preambule to get preambule
let preambule = "start:\n zkPC + 2 => RR\n :JMP(main)\n :JMP(finalizeExecution)";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nits:

  1. "preambule" -> "preamble"
  2. Let's make it more readable by splitting across multiple lines:
let preamble = "\
start:
  zkPC + 2 => RR
  :JMP(main)
  :JMP(finalizeExecution)";

];
for invoce in invocations {
// TODO: remove this .clone()
main.append(&mut invoce.clone());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to use Vec::extend for this with something like

main.extend(invocations.iter().cloned())

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

main.extend() expects Vec<String> while invocations is Vec<Vec<String>>. And I'm not sure but I guess this .cloned() performs same amount of copying as .clone(), isn't it?

But

for invocation in invocations {
    main.extend(invocation);
}

works.

}
main.push(" SP - 1 => SP".to_string());
main.push(" :JMP(RR)".to_string());
let mut postabmule = generate_postamble();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: "postabmule" -> "postamble"

if let Some(command) = parse_run_command(comment.text, &func.signature)? {
match command {
RunCommand::Print(_) => {
unreachable!()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a todo!(), right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I don't know really now. I didn't see any Print command yet. And if we will somehow adopt cranelift tests to zkasm (for example remove test interpret from the header) removing print commands is normal strategy imo. But maybe todo!() is really better here

@MCJOHN974
Copy link
Author

Last commit fixes review notes (thanks @Akashin). Btw, I see that main fails windows tests at zkasm_runner.rs as well, so we just ignore them, am I right?

@aborg-dev
Copy link

Last commit fixes review notes (thanks @Akashin). Btw, I see that main fails windows tests at zkasm_runner.rs as well, so we just ignore them, am I right?

I'm working on fixing windows tests, but this should not block submitting this PR.

Comment on lines 17 to 18
; run: %add_i64(0, 0) == 0
; run: %add_i64(0, 1) == -1
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't these call %sub_i64?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thanks (= But this file is not supposed to really test something, it is more for me to debug the filetest

Comment on lines 267 to 268
// TODO: this function should be much rewrited,
// now it works for one very basic case
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could describe the one case it works for and outline how it needs to be rewritten.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It must be rewritten before we will migrate to this infra (optimistically before end of Feb), and I think nobody except me will do this, so this comment is just a signal for reviewers to not ask replace this assembling to something better, because I will do it in next PRs

// TODO: this function should be much rewrited,
// now it works for one very basic case
pub fn build_test_zkasm(functions: Vec<Vec<String>>, invocations: Vec<Vec<String>>) -> String {
// TODO: use generate_preambule to get preambule
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// TODO: use generate_preambule to get preambule
// TODO: use generate_preamble to get preamble

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@MCJOHN974
Copy link
Author

Thanks, @mooori, this commit fixes notes you find

@MCJOHN974
Copy link
Author

I'm working on fixing windows tests, but this should not block submitting this PR.

Thanks! I bet merging this PR is not blocking me and we can wait windows tests fix

@MCJOHN974
Copy link
Author

@Akashin, now CI is green and all review notes marked as outdated. Should we merge this?

Copy link

@aborg-dev aborg-dev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, let's ship this!

@MCJOHN974 MCJOHN974 added this pull request to the merge queue Feb 22, 2024
Merged via the queue into main with commit d174ee5 Feb 22, 2024
31 checks passed
@MCJOHN974 MCJOHN974 deleted the viktar/filetests branch February 22, 2024 12:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants