Skip to content

Commit

Permalink
use ast::Value equivalence, not enum equality, to do constant folding…
Browse files Browse the repository at this point in the history
… of constants (#12518)

Some apparently different `ast::Value` values may represent the same runtime values
(Value::ByteArray(..) and Value::AddressArray(..) values can each also be represented by Value::Vector(..) instead)
and some may not be well-defined at compile time (different symbolic addresses could end up the same).
  • Loading branch information
brmataptos authored Mar 15, 2024
1 parent 3246c4a commit 1b43e0e
Show file tree
Hide file tree
Showing 13 changed files with 458 additions and 48 deletions.
23 changes: 11 additions & 12 deletions third_party/move/move-compiler-v2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,16 @@ pub fn run_checker_and_rewriters(
options: Options,
scope: RewritingScope,
) -> anyhow::Result<GlobalEnv> {
let env_pipeline = check_and_rewrite_pipeline(&options, false, scope);
let optimize_on = options.experiment_on(Experiment::OPTIMIZE);
let mut env_pipeline = check_and_rewrite_pipeline(&options, false, scope);
env_pipeline.add("simplifier", {
move |env: &mut GlobalEnv| {
ast_simplifier::run_simplifier(
env,
optimize_on, // eliminate code only if optimize is on
)
}
});
let mut env = run_checker(options)?;
if !env.has_errors() {
env_pipeline.run(&mut env);
Expand Down Expand Up @@ -209,12 +218,10 @@ pub fn run_file_format_gen(env: &GlobalEnv, targets: &FunctionTargetsHolder) ->
/// `Everything` for use with the Move Prover, otherwise `CompilationTarget`
/// should be used.
pub fn check_and_rewrite_pipeline<'a>(
options: &Options,
_options: &Options,
for_v1_model: bool,
inlining_scope: RewritingScope,
) -> EnvProcessorPipeline<'a> {
let optimize_on = options.experiment_on(Experiment::OPTIMIZE);

// The default transformation pipeline on the GlobalEnv
let mut env_pipeline = EnvProcessorPipeline::default();
env_pipeline.add(
Expand Down Expand Up @@ -245,14 +252,6 @@ pub fn check_and_rewrite_pipeline<'a>(
|env: &mut GlobalEnv| function_checker::check_access_and_use(env, false),
);
}
env_pipeline.add("simplifier", {
move |env: &mut GlobalEnv| {
ast_simplifier::run_simplifier(
env,
optimize_on, // eliminate code only if optimize is on
)
}
});
env_pipeline.add("specification checker", |env| {
let env: &GlobalEnv = env;
spec_checker::run_spec_checker(env)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// -- Model dump before bytecode pipeline
module 0xcafe::Ristretto {
public fun test() {
{
let non_canonical_highbit: vector<u8> = Vector<u8>(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128);
{
let non_canonical_highbit_hex: vector<u8> = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128];
if Eq<vector<u8>>(non_canonical_highbit, non_canonical_highbit_hex) {
Tuple()
} else {
Abort(1)
};
Tuple()
}
}
}
} // end 0xcafe::Ristretto
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//# publish --print-bytecode
module 0xcafe::Ristretto {
public fun test() {
let non_canonical_highbit = vector[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128];
let non_canonical_highbit_hex = x"0000000000000000000000000000000000000000000000000000000000000080";
assert!(non_canonical_highbit == non_canonical_highbit_hex, 1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// -- Model dump before bytecode pipeline
module 0xcafe::Ristretto {
public fun test() {
if true {
Tuple()
} else {
Abort(1)
};
Tuple()
}
} // end 0xcafe::Ristretto
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//# publish --print-bytecode
module 0xcafe::Ristretto {
public fun test() {
let non_canonical_highbit = vector[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128];
let non_canonical_highbit_hex = x"0000000000000000000000000000000000000000000000000000000000000080";
assert!(non_canonical_highbit == non_canonical_highbit_hex, 1);
}
}
50 changes: 16 additions & 34 deletions third_party/move/move-compiler-v2/tests/testsuite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,49 +110,31 @@ impl TestConfig {

// The bytecode transformation pipeline
let mut pipeline = FunctionTargetPipeline::default();

// Get path to allow path-specific test configuration
let path = path.to_string_lossy();
if path.contains("/inlining/")
|| path.contains("/folding/")
|| path.contains("/simplifier/")
{

// turn on simplifier unless doing no-simplifier tests.
if path.contains("/simplifier-elimination/") {
env_pipeline.add("simplifier", |env: &mut GlobalEnv| {
ast_simplifier::run_simplifier(
env, false, // No code elimination
env, true, // Code elimination
)
});
pipeline.add_processor(Box::new(LiveVarAnalysisProcessor {}));
pipeline.add_processor(Box::new(ReferenceSafetyProcessor {}));
pipeline.add_processor(Box::new(ExitStateAnalysisProcessor {}));
pipeline.add_processor(Box::new(AbilityProcessor {}));
Self {
stop_before_generating_bytecode: false,
dump_ast: AstDumpLevel::EndStage,
env_pipeline,
pipeline,
generate_file_format: false,
dump_annotated_targets: false,
dump_for_only_some_stages: None,
}
} else if path.contains("/simplifier-elimination/") {
} else if !path.contains("/no-simplifier/") {
env_pipeline.add("simplifier", |env: &mut GlobalEnv| {
ast_simplifier::run_simplifier(
env, true, // Enable code elimination
env, false, // No code elimination
)
});
pipeline.add_processor(Box::new(LiveVarAnalysisProcessor {}));
pipeline.add_processor(Box::new(ReferenceSafetyProcessor {}));
pipeline.add_processor(Box::new(ExitStateAnalysisProcessor {}));
pipeline.add_processor(Box::new(AbilityProcessor {}));
Self {
stop_before_generating_bytecode: false,
dump_ast: AstDumpLevel::EndStage,
env_pipeline,
pipeline,
generate_file_format: false,
dump_annotated_targets: false,
dump_for_only_some_stages: None,
}
} else if path.contains("/no-simplifier/") {
}

if path.contains("/inlining/")
|| path.contains("/folding/")
|| path.contains("/simplifier/")
|| path.contains("/simplifier-elimination/")
|| path.contains("/no-simplifier/")
{
pipeline.add_processor(Box::new(LiveVarAnalysisProcessor {}));
pipeline.add_processor(Box::new(ReferenceSafetyProcessor {}));
pipeline.add_processor(Box::new(ExitStateAnalysisProcessor {}));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
processed 1 task

task 0 'publish'. lines 1-8:



==> Compiler v2 delivered same results!

>>> V1 Compiler {
== BEGIN Bytecode ==
// Move bytecode v6
module cafe.Ristretto {


public test() /* def_idx: 0 */ {
B0:
0: Ret
}
}
== END Bytecode ==
}

>>> V2 Compiler {
== BEGIN Bytecode ==
// Move bytecode v7
module cafe.Ristretto {


public test() /* def_idx: 0 */ {
B0:
0: LdTrue
1: BrFalse(3)
B1:
2: Branch(5)
B2:
3: LdU64(1)
4: Abort
B3:
5: Ret
}
}
== END Bytecode ==
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//# publish --print-bytecode
module 0xcafe::Ristretto {
public fun test() {
let non_canonical_highbit = vector[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128];
let non_canonical_highbit_hex = x"0000000000000000000000000000000000000000000000000000000000000080";
assert!(non_canonical_highbit == non_canonical_highbit_hex, 1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
processed 1 task

task 0 'publish'. lines 1-8:



>>> V1 Compiler {
== BEGIN Bytecode ==
// Move bytecode v6
module cafe.Ristretto {


public test() /* def_idx: 0 */ {
B0:
0: Ret
}
}
== END Bytecode ==
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//# publish --print-bytecode
module 0xcafe::Ristretto {
public fun test() {
let non_canonical_highbit = vector[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128];
let non_canonical_highbit_hex = x"0000000000000000000000000000000000000000000000000000000000000080";
assert!(non_canonical_highbit == non_canonical_highbit_hex, 1);
}
}
Loading

0 comments on commit 1b43e0e

Please sign in to comment.