Skip to content

Commit 92f028d

Browse files
committed
Code refactor and comment update
1 parent 7b582f1 commit 92f028d

3 files changed

Lines changed: 35 additions & 49 deletions

File tree

build_system/src/test.rs

Lines changed: 31 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -318,21 +318,20 @@ fn maybe_run_command_in_vm(
318318
Ok(())
319319
}
320320

321-
fn no_builtins_tests(env: &Env, args: &TestArg) -> Result<(), String> {
322-
// Test that the #![no_builtins] attribute prevents GCC from replacing
323-
// code patterns (like loops) with calls to builtins (like memset).
324-
// See https://github.com/rust-lang/rustc_codegen_gcc/issues/570
325-
321+
/// Compile a source file to an object file and check if it contains a memset reference.
322+
fn object_has_memset(
323+
env: &Env,
324+
args: &TestArg,
325+
src_file: &str,
326+
obj_file_name: &str,
327+
) -> Result<bool, String> {
326328
let cargo_target_dir = Path::new(&args.config_info.cargo_target_dir);
327-
328-
// Test 1: WITH #![no_builtins] - memset should NOT be present
329-
println!("[TEST] no_builtins attribute (with #![no_builtins])");
330-
let obj_file = cargo_target_dir.join("no_builtins_test.o");
329+
let obj_file = cargo_target_dir.join(obj_file_name);
331330
let obj_file_str = obj_file.to_str().expect("obj_file to_str");
332331

333332
let mut command = args.config_info.rustc_command_vec();
334333
command.extend_from_slice(&[
335-
&"tests/no_builtins/no_builtins.rs",
334+
&src_file,
336335
&"--emit",
337336
&"obj",
338337
&"-O",
@@ -346,47 +345,34 @@ fn no_builtins_tests(env: &Env, args: &TestArg) -> Result<(), String> {
346345
let nm_output = run_command_with_env(&[&"nm", &obj_file_str], None, Some(env))?;
347346
let nm_stdout = String::from_utf8_lossy(&nm_output.stdout);
348347

349-
if nm_stdout.contains("memset") {
350-
return Err(format!(
351-
"no_builtins test FAILED: Found 'memset' in object file.\n\
348+
Ok(nm_stdout.contains("memset"))
349+
}
350+
351+
fn no_builtins_tests(env: &Env, args: &TestArg) -> Result<(), String> {
352+
// Test that the #![no_builtins] attribute prevents GCC from replacing
353+
// code patterns (like loops) with calls to builtins (like memset).
354+
// See https://github.com/rust-lang/rustc_codegen_gcc/issues/570
355+
356+
// Test 1: WITH #![no_builtins] - memset should NOT be present
357+
println!("[TEST] no_builtins attribute (with #![no_builtins])");
358+
let has_memset =
359+
object_has_memset(env, args, "tests/no_builtins/no_builtins.rs", "no_builtins_test.o")?;
360+
if has_memset {
361+
return Err("no_builtins test FAILED: Found 'memset' in object file.\n\
352362
The #![no_builtins] attribute should prevent GCC from replacing \n\
353-
code patterns with builtin calls.\n\
354-
nm output:\n{}",
355-
nm_stdout
356-
));
363+
code patterns with builtin calls."
364+
.to_string());
357365
}
358-
println!("[TEST] no_builtins attribute (with #![no_builtins]): PASSED");
359366

360367
// Test 2: WITHOUT #![no_builtins] - memset SHOULD be present
361368
println!("[TEST] no_builtins attribute (without #![no_builtins])");
362-
let obj_file = cargo_target_dir.join("with_builtins_test.o");
363-
let obj_file_str = obj_file.to_str().expect("obj_file to_str");
364-
365-
let mut command = args.config_info.rustc_command_vec();
366-
command.extend_from_slice(&[
367-
&"tests/no_builtins/with_builtins.rs",
368-
&"--emit",
369-
&"obj",
370-
&"-O",
371-
&"--target",
372-
&args.config_info.target_triple,
373-
&"-o",
374-
]);
375-
command.push(&obj_file_str);
376-
run_command_with_env(&command, None, Some(env))?;
377-
378-
let nm_output = run_command_with_env(&[&"nm", &obj_file_str], None, Some(env))?;
379-
let nm_stdout = String::from_utf8_lossy(&nm_output.stdout);
380-
381-
if !nm_stdout.contains("memset") {
382-
return Err(format!(
383-
"no_builtins test FAILED: 'memset' NOT found in object file.\n\
384-
Without #![no_builtins], GCC should replace the loop with memset.\n\
385-
nm output:\n{}",
386-
nm_stdout
387-
));
369+
let has_memset =
370+
object_has_memset(env, args, "tests/no_builtins/with_builtins.rs", "with_builtins_test.o")?;
371+
if !has_memset {
372+
return Err("no_builtins test FAILED: 'memset' NOT found in object file.\n\
373+
Without #![no_builtins], GCC should replace the loop with memset."
374+
.to_string());
388375
}
389-
println!("[TEST] no_builtins attribute (without #![no_builtins]): PASSED");
390376

391377
Ok(())
392378
}

src/base.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ pub fn compile_codegen_unit(
139139

140140
// NOTE: We need to honor the `#![no_builtins]` attribute to prevent GCC from
141141
// replacing code patterns (like loops) with calls to builtins (like memset).
142-
// This is important for crates like `compiler_builtins` that implement these functions.
143-
// See https://github.com/rust-lang/rustc_codegen_gcc/issues/570
142+
// The `-fno-tree-loop-distribute-patterns` flag disables the loop distribution pass
143+
// that transforms loops into calls to library functions (memset, memcpy, etc.).
144144
let crate_attrs = tcx.hir_attrs(rustc_hir::CRATE_HIR_ID);
145145
if find_attr!(crate_attrs, AttributeKind::NoBuiltins) {
146146
context.add_command_line_option("-fno-tree-loop-distribute-patterns");

tests/no_builtins/no_builtins.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Test that the #![no_builtins] attribute is honored.
2-
// When this attribute is present, GCC should pass -fno-builtin to prevent
3-
// replacing code patterns (like loops) with calls to builtins (like memset).
2+
// When this attribute is present, GCC should not replace code patterns
3+
// (like loops) with calls to builtins (like memset).
44
// See https://github.com/rust-lang/rustc_codegen_gcc/issues/570
55
//
66
// This test is verified by the build system test `--no-builtins-tests` which

0 commit comments

Comments
 (0)