Skip to content

Commit 10921ab

Browse files
committed
Fix #570: Honor #![no_builtins] attribute by passing -fno-builtin to GCC
1 parent 0be3954 commit 10921ab

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/base.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::sync::Arc;
44
use std::time::Instant;
55

66
use gccjit::{CType, Context, FunctionType, GlobalKind};
7+
use rustc_ast::attr;
78
use rustc_codegen_ssa::ModuleCodegen;
89
use rustc_codegen_ssa::base::maybe_create_entry_wrapper;
910
use rustc_codegen_ssa::mono_item::MonoItemExt;
@@ -14,7 +15,7 @@ use rustc_middle::dep_graph;
1415
use rustc_middle::mir::mono::Visibility;
1516
use rustc_middle::ty::TyCtxt;
1617
use rustc_session::config::DebugInfo;
17-
use rustc_span::Symbol;
18+
use rustc_span::{Symbol, sym};
1819
#[cfg(feature = "master")]
1920
use rustc_target::spec::SymbolVisibility;
2021
use rustc_target::spec::{Arch, RelocModel};
@@ -136,6 +137,15 @@ pub fn compile_codegen_unit(
136137
// NOTE: Rust relies on LLVM doing wrapping on overflow.
137138
context.add_command_line_option("-fwrapv");
138139

140+
// NOTE: We need to honor the `#![no_builtins]` attribute to prevent GCC from
141+
// 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
144+
let crate_attrs = tcx.hir_attrs(rustc_hir::CRATE_HIR_ID);
145+
if attr::contains_name(crate_attrs, sym::no_builtins) {
146+
context.add_command_line_option("-fno-builtin");
147+
}
148+
139149
if let Some(model) = tcx.sess.code_model() {
140150
use rustc_target::spec::CodeModel;
141151

0 commit comments

Comments
 (0)