Remove module-level code generation tests (#5870)

* Remove module-level code generation tests

* Add cold block tests for each backend

* Better cold block tests
This commit is contained in:
Trevor Elliott
2023-02-23 17:19:26 -08:00
committed by GitHub
parent f91640ffab
commit c5d9d5b10f
8 changed files with 309 additions and 662 deletions

View File

@@ -211,130 +211,3 @@ pub fn isa_builder(triple: Triple) -> IsaBuilder {
},
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::cursor::{Cursor, FuncCursor};
use crate::dominator_tree::DominatorTree;
use crate::flowgraph::ControlFlowGraph;
use crate::ir::types::*;
use crate::ir::UserFuncName;
use crate::ir::{AbiParam, Function, InstBuilder, Signature};
use crate::isa::CallConv;
use crate::settings;
use crate::settings::Configurable;
use core::str::FromStr;
use target_lexicon::Triple;
#[test]
fn test_compile_function() {
let name = UserFuncName::testcase("test0");
let mut sig = Signature::new(CallConv::SystemV);
sig.params.push(AbiParam::new(I32));
sig.returns.push(AbiParam::new(I32));
let mut func = Function::with_name_signature(name, sig);
let bb0 = func.dfg.make_block();
let arg0 = func.dfg.append_block_param(bb0, I32);
let mut pos = FuncCursor::new(&mut func);
pos.insert_block(bb0);
let v0 = pos.ins().iconst(I32, 0x1234);
let v1 = pos.ins().iadd(arg0, v0);
pos.ins().return_(&[v1]);
let mut shared_flags_builder = settings::builder();
shared_flags_builder.set("opt_level", "none").unwrap();
let shared_flags = settings::Flags::new(shared_flags_builder);
let isa_flags = s390x_settings::Flags::new(&shared_flags, s390x_settings::builder());
let backend = S390xBackend::new_with_flags(
Triple::from_str("s390x").unwrap(),
shared_flags,
isa_flags,
);
let cfg = ControlFlowGraph::with_function(&func);
let domtree = DominatorTree::with_function(&func, &cfg);
let result = backend
.compile_function(&mut func, &domtree, /* want_disasm = */ false)
.unwrap();
let code = result.buffer.data();
// ahi %r2, 0x1234
// br %r14
let golden = vec![0xa7, 0x2a, 0x12, 0x34, 0x07, 0xfe];
assert_eq!(code, &golden[..]);
}
#[test]
fn test_branch_lowering() {
let name = UserFuncName::testcase("test0");
let mut sig = Signature::new(CallConv::SystemV);
sig.params.push(AbiParam::new(I32));
sig.returns.push(AbiParam::new(I32));
let mut func = Function::with_name_signature(name, sig);
let bb0 = func.dfg.make_block();
let arg0 = func.dfg.append_block_param(bb0, I32);
let bb1 = func.dfg.make_block();
let bb2 = func.dfg.make_block();
let bb3 = func.dfg.make_block();
let mut pos = FuncCursor::new(&mut func);
pos.insert_block(bb0);
let v0 = pos.ins().iconst(I32, 0x1234);
let v1 = pos.ins().iadd(arg0, v0);
pos.ins().brif(v1, bb1, &[], bb2, &[]);
pos.insert_block(bb1);
pos.ins().brif(v1, bb2, &[], bb3, &[]);
pos.insert_block(bb2);
let v2 = pos.ins().iadd(v1, v0);
pos.ins().brif(v2, bb2, &[], bb1, &[]);
pos.insert_block(bb3);
let v3 = pos.ins().isub(v1, v0);
pos.ins().return_(&[v3]);
let mut shared_flags_builder = settings::builder();
shared_flags_builder.set("opt_level", "none").unwrap();
let shared_flags = settings::Flags::new(shared_flags_builder);
let isa_flags = s390x_settings::Flags::new(&shared_flags, s390x_settings::builder());
let backend = S390xBackend::new_with_flags(
Triple::from_str("s390x").unwrap(),
shared_flags,
isa_flags,
);
let cfg = ControlFlowGraph::with_function(&func);
let domtree = DominatorTree::with_function(&func, &cfg);
let result = backend
.compile_function(&mut func, &domtree, /* want_disasm = */ false)
.unwrap();
let code = result.buffer.data();
// FIXME: the branching logic should be optimized more
// To update this comment, write the golden bytes to a file, and run the following command
// on it to update:
// > s390x-linux-gnu-objdump -b binary -D <file> -m s390
//
// 0: a7 2a 12 34 ahi %r2,4660
// 4: a7 2e 00 00 chi %r2,0
// 8: c0 94 00 00 00 0b jgnlh 0x1e
// e: a7 2e 00 00 chi %r2,0
// 12: c0 64 00 00 00 06 jglh 0x1e
// 18: a7 2a ed cc ahi %r2,-4660
// 1c: 07 fe br %r14
// 1e: ec 32 12 34 00 d8 ahik %r3,%r2,4660
// 24: a7 3e 00 00 chi %r3,0
// 28: c0 64 ff ff ff fb jglh 0x1e
// 2e: c0 f4 ff ff ff f0 jg 0xe
let golden = vec![
167, 42, 18, 52, 167, 46, 0, 0, 192, 148, 0, 0, 0, 11, 167, 46, 0, 0, 192, 100, 0, 0,
0, 6, 167, 42, 237, 204, 7, 254, 236, 50, 18, 52, 0, 216, 167, 62, 0, 0, 192, 100, 255,
255, 255, 251, 192, 244, 255, 255, 255, 240,
];
assert_eq!(code, &golden[..]);
}
}