Skip to content

Commit e3c1cc0

Browse files
committed
Move most flags from module_codegen to new_context
This way they also apply to the allocator shim.
1 parent daf8bb9 commit e3c1cc0

File tree

4 files changed

+143
-147
lines changed

4 files changed

+143
-147
lines changed

src/back/write.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use rustc_log::tracing::debug;
1111
use rustc_session::config::OutputType;
1212
use rustc_target::spec::SplitDebuginfo;
1313

14-
use crate::base::add_pic_option;
1514
use crate::errors::CopyBitcode;
15+
use crate::gcc_util::add_pic_option;
1616
use crate::{GccContext, LtoMode};
1717

1818
pub(crate) fn codegen(

src/base.rs

Lines changed: 4 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
use std::collections::HashSet;
2-
use std::env;
31
use std::sync::Arc;
42
use std::time::Instant;
53

6-
use gccjit::{CType, Context, FunctionType, GlobalKind};
4+
use gccjit::{CType, FunctionType, GlobalKind};
75
use rustc_codegen_ssa::ModuleCodegen;
86
use rustc_codegen_ssa::base::maybe_create_entry_wrapper;
97
use rustc_codegen_ssa::mono_item::MonoItemExt;
@@ -18,11 +16,11 @@ use rustc_session::config::DebugInfo;
1816
use rustc_span::Symbol;
1917
#[cfg(feature = "master")]
2018
use rustc_target::spec::SymbolVisibility;
21-
use rustc_target::spec::{Arch, RelocModel};
2219

2320
use crate::builder::Builder;
2421
use crate::context::CodegenCx;
25-
use crate::{GccContext, LockedTargetInfo, LtoMode, SyncContext, gcc_util, new_context};
22+
use crate::gcc_util::new_context;
23+
use crate::{GccContext, LockedTargetInfo, LtoMode, SyncContext};
2624

2725
#[cfg(feature = "master")]
2826
pub fn visibility_to_gcc(visibility: Visibility) -> gccjit::Visibility {
@@ -101,41 +99,7 @@ pub fn compile_codegen_unit(
10199
) -> ModuleCodegen<GccContext> {
102100
let cgu = tcx.codegen_unit(cgu_name);
103101
// Instantiate monomorphizations without filling out definitions yet...
104-
let context = new_context(tcx);
105-
106-
if tcx.sess.panic_strategy().unwinds() {
107-
context.add_command_line_option("-fexceptions");
108-
context.add_driver_option("-fexceptions");
109-
}
110-
111-
let disabled_features: HashSet<_> = tcx
112-
.sess
113-
.opts
114-
.cg
115-
.target_feature
116-
.split(',')
117-
.filter(|feature| feature.starts_with('-'))
118-
.map(|string| &string[1..])
119-
.collect();
120-
121-
if !disabled_features.contains("avx") && tcx.sess.target.arch == Arch::X86_64 {
122-
// NOTE: we always enable AVX because the equivalent of llvm.x86.sse2.cmp.pd in GCC for
123-
// SSE2 is multiple builtins, so we use the AVX __builtin_ia32_cmppd instead.
124-
// FIXME(antoyo): use the proper builtins for llvm.x86.sse2.cmp.pd and similar.
125-
context.add_command_line_option("-mavx");
126-
}
127-
128-
for arg in &tcx.sess.opts.cg.llvm_args {
129-
context.add_command_line_option(arg);
130-
}
131-
// NOTE: This is needed to compile the file src/intrinsic/archs.rs during a bootstrap of rustc.
132-
context.add_command_line_option("-fno-var-tracking-assignments");
133-
// NOTE: an optimization (https://github.com/rust-lang/rustc_codegen_gcc/issues/53).
134-
context.add_command_line_option("-fno-semantic-interposition");
135-
// NOTE: Rust relies on LLVM not doing TBAA (https://github.com/rust-lang/unsafe-code-guidelines/issues/292).
136-
context.add_command_line_option("-fno-strict-aliasing");
137-
// NOTE: Rust relies on LLVM doing wrapping on overflow.
138-
context.add_command_line_option("-fwrapv");
102+
let context = new_context(tcx.sess);
139103

140104
// NOTE: We need to honor the `#![no_builtins]` attribute to prevent GCC from
141105
// replacing code patterns (like loops) with calls to builtins (like memset).
@@ -148,64 +112,6 @@ pub fn compile_codegen_unit(
148112
context.add_command_line_option("-fno-tree-loop-distribute-patterns");
149113
}
150114

151-
if let Some(model) = tcx.sess.code_model() {
152-
use rustc_target::spec::CodeModel;
153-
154-
context.add_command_line_option(match model {
155-
CodeModel::Tiny => "-mcmodel=tiny",
156-
CodeModel::Small => "-mcmodel=small",
157-
CodeModel::Kernel => "-mcmodel=kernel",
158-
CodeModel::Medium => "-mcmodel=medium",
159-
CodeModel::Large => "-mcmodel=large",
160-
});
161-
}
162-
163-
add_pic_option(&context, tcx.sess.relocation_model());
164-
165-
let target_cpu = gcc_util::target_cpu(tcx.sess);
166-
if target_cpu != "generic" {
167-
context.add_command_line_option(format!("-march={}", target_cpu));
168-
}
169-
170-
if tcx
171-
.sess
172-
.opts
173-
.unstable_opts
174-
.function_sections
175-
.unwrap_or(tcx.sess.target.function_sections)
176-
{
177-
context.add_command_line_option("-ffunction-sections");
178-
context.add_command_line_option("-fdata-sections");
179-
}
180-
181-
if env::var("CG_GCCJIT_DUMP_RTL").as_deref() == Ok("1") {
182-
context.add_command_line_option("-fdump-rtl-vregs");
183-
}
184-
if env::var("CG_GCCJIT_DUMP_RTL_ALL").as_deref() == Ok("1") {
185-
context.add_command_line_option("-fdump-rtl-all");
186-
}
187-
if env::var("CG_GCCJIT_DUMP_TREE_ALL").as_deref() == Ok("1") {
188-
context.add_command_line_option("-fdump-tree-all-eh");
189-
}
190-
if env::var("CG_GCCJIT_DUMP_IPA_ALL").as_deref() == Ok("1") {
191-
context.add_command_line_option("-fdump-ipa-all-eh");
192-
}
193-
if env::var("CG_GCCJIT_DUMP_CODE").as_deref() == Ok("1") {
194-
context.set_dump_code_on_compile(true);
195-
}
196-
if env::var("CG_GCCJIT_DUMP_GIMPLE").as_deref() == Ok("1") {
197-
context.set_dump_initial_gimple(true);
198-
}
199-
if env::var("CG_GCCJIT_DUMP_EVERYTHING").as_deref() == Ok("1") {
200-
context.set_dump_everything(true);
201-
}
202-
if env::var("CG_GCCJIT_KEEP_INTERMEDIATES").as_deref() == Ok("1") {
203-
context.set_keep_intermediates(true);
204-
}
205-
if env::var("CG_GCCJIT_VERBOSE").as_deref() == Ok("1") {
206-
context.add_driver_option("-v");
207-
}
208-
209115
// NOTE: The codegen generates unreachable blocks.
210116
context.set_allow_unreachable_blocks(true);
211117

@@ -269,24 +175,3 @@ pub fn compile_codegen_unit(
269175

270176
(module, cost)
271177
}
272-
273-
pub fn add_pic_option<'gcc>(context: &Context<'gcc>, relocation_model: RelocModel) {
274-
match relocation_model {
275-
rustc_target::spec::RelocModel::Static => {
276-
context.add_command_line_option("-fno-pie");
277-
context.add_driver_option("-fno-pie");
278-
}
279-
rustc_target::spec::RelocModel::Pic => {
280-
context.add_command_line_option("-fPIC");
281-
// NOTE: we use both add_command_line_option and add_driver_option because the usage in
282-
// this module (compile_codegen_unit) requires add_command_line_option while the usage
283-
// in the back::write module (codegen) requires add_driver_option.
284-
context.add_driver_option("-fPIC");
285-
}
286-
rustc_target::spec::RelocModel::Pie => {
287-
context.add_command_line_option("-fPIE");
288-
context.add_driver_option("-fPIE");
289-
}
290-
model => eprintln!("Unsupported relocation model: {:?}", model),
291-
}
292-
}

src/gcc_util.rs

Lines changed: 134 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
#[cfg(feature = "master")]
1+
use std::collections::HashSet;
2+
use std::env;
3+
24
use gccjit::Context;
5+
#[cfg(feature = "master")]
6+
use gccjit::Version;
37
use rustc_codegen_ssa::target_features;
48
use rustc_data_structures::smallvec::{SmallVec, smallvec};
59
use rustc_session::Session;
6-
use rustc_target::spec::Arch;
10+
use rustc_target::spec::{Arch, RelocModel};
711

812
fn gcc_features_by_flags(sess: &Session, features: &mut Vec<String>) {
913
target_features::retpoline_features_by_flags(sess, features);
@@ -135,3 +139,131 @@ pub fn target_cpu(sess: &Session) -> &str {
135139
None => handle_native(sess.target.cpu.as_ref()),
136140
}
137141
}
142+
143+
pub fn new_context<'gcc>(sess: &Session) -> Context<'gcc> {
144+
let context = Context::default();
145+
if matches!(sess.target.arch, Arch::X86 | Arch::X86_64) {
146+
context.add_command_line_option("-masm=intel");
147+
}
148+
#[cfg(feature = "master")]
149+
{
150+
context.set_special_chars_allowed_in_func_names("$.*");
151+
let version = Version::get();
152+
let version = format!("{}.{}.{}", version.major, version.minor, version.patch);
153+
context.set_output_ident(&format!(
154+
"rustc version {} with libgccjit {}",
155+
rustc_interface::util::rustc_version_str().unwrap_or("unknown version"),
156+
version,
157+
));
158+
}
159+
// TODO(antoyo): check if this should only be added when using -Cforce-unwind-tables=n.
160+
context.add_command_line_option("-fno-asynchronous-unwind-tables");
161+
162+
if sess.panic_strategy().unwinds() {
163+
context.add_command_line_option("-fexceptions");
164+
context.add_driver_option("-fexceptions");
165+
}
166+
167+
let disabled_features: HashSet<_> = sess
168+
.opts
169+
.cg
170+
.target_feature
171+
.split(',')
172+
.filter(|feature| feature.starts_with('-'))
173+
.map(|string| &string[1..])
174+
.collect();
175+
176+
if !disabled_features.contains("avx") && sess.target.arch == Arch::X86_64 {
177+
// NOTE: we always enable AVX because the equivalent of llvm.x86.sse2.cmp.pd in GCC for
178+
// SSE2 is multiple builtins, so we use the AVX __builtin_ia32_cmppd instead.
179+
// FIXME(antoyo): use the proper builtins for llvm.x86.sse2.cmp.pd and similar.
180+
context.add_command_line_option("-mavx");
181+
}
182+
183+
for arg in &sess.opts.cg.llvm_args {
184+
context.add_command_line_option(arg);
185+
}
186+
// NOTE: This is needed to compile the file src/intrinsic/archs.rs during a bootstrap of rustc.
187+
context.add_command_line_option("-fno-var-tracking-assignments");
188+
// NOTE: an optimization (https://github.com/rust-lang/rustc_codegen_gcc/issues/53).
189+
context.add_command_line_option("-fno-semantic-interposition");
190+
// NOTE: Rust relies on LLVM not doing TBAA (https://github.com/rust-lang/unsafe-code-guidelines/issues/292).
191+
context.add_command_line_option("-fno-strict-aliasing");
192+
// NOTE: Rust relies on LLVM doing wrapping on overflow.
193+
context.add_command_line_option("-fwrapv");
194+
195+
if let Some(model) = sess.code_model() {
196+
use rustc_target::spec::CodeModel;
197+
198+
context.add_command_line_option(match model {
199+
CodeModel::Tiny => "-mcmodel=tiny",
200+
CodeModel::Small => "-mcmodel=small",
201+
CodeModel::Kernel => "-mcmodel=kernel",
202+
CodeModel::Medium => "-mcmodel=medium",
203+
CodeModel::Large => "-mcmodel=large",
204+
});
205+
}
206+
207+
add_pic_option(&context, sess.relocation_model());
208+
209+
let target_cpu = target_cpu(sess);
210+
if target_cpu != "generic" {
211+
context.add_command_line_option(format!("-march={}", target_cpu));
212+
}
213+
214+
if sess.opts.unstable_opts.function_sections.unwrap_or(sess.target.function_sections) {
215+
context.add_command_line_option("-ffunction-sections");
216+
context.add_command_line_option("-fdata-sections");
217+
}
218+
219+
if env::var("CG_GCCJIT_DUMP_RTL").as_deref() == Ok("1") {
220+
context.add_command_line_option("-fdump-rtl-vregs");
221+
}
222+
if env::var("CG_GCCJIT_DUMP_RTL_ALL").as_deref() == Ok("1") {
223+
context.add_command_line_option("-fdump-rtl-all");
224+
}
225+
if env::var("CG_GCCJIT_DUMP_TREE_ALL").as_deref() == Ok("1") {
226+
context.add_command_line_option("-fdump-tree-all-eh");
227+
}
228+
if env::var("CG_GCCJIT_DUMP_IPA_ALL").as_deref() == Ok("1") {
229+
context.add_command_line_option("-fdump-ipa-all-eh");
230+
}
231+
if env::var("CG_GCCJIT_DUMP_CODE").as_deref() == Ok("1") {
232+
context.set_dump_code_on_compile(true);
233+
}
234+
if env::var("CG_GCCJIT_DUMP_GIMPLE").as_deref() == Ok("1") {
235+
context.set_dump_initial_gimple(true);
236+
}
237+
if env::var("CG_GCCJIT_DUMP_EVERYTHING").as_deref() == Ok("1") {
238+
context.set_dump_everything(true);
239+
}
240+
if env::var("CG_GCCJIT_KEEP_INTERMEDIATES").as_deref() == Ok("1") {
241+
context.set_keep_intermediates(true);
242+
}
243+
if env::var("CG_GCCJIT_VERBOSE").as_deref() == Ok("1") {
244+
context.add_driver_option("-v");
245+
}
246+
247+
context
248+
}
249+
250+
pub fn add_pic_option<'gcc>(context: &Context<'gcc>, relocation_model: RelocModel) {
251+
match relocation_model {
252+
rustc_target::spec::RelocModel::Static => {
253+
context.add_command_line_option("-fno-pie");
254+
context.add_driver_option("-fno-pie");
255+
}
256+
rustc_target::spec::RelocModel::Pic => {
257+
context.add_command_line_option("-fPIC");
258+
// NOTE: we use both add_command_line_option and add_driver_option because the usage in
259+
// this module (compile_codegen_unit) requires add_command_line_option while the usage
260+
// in the back::write module (codegen) requires add_driver_option.
261+
context.add_driver_option("-fPIC");
262+
}
263+
rustc_target::spec::RelocModel::Pie => {
264+
context.add_command_line_option("-fPIE");
265+
context.add_driver_option("-fPIE");
266+
}
267+
model => eprintln!("Unsupported relocation model: {:?}", model),
268+
}
269+
}

src/lib.rs

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ use std::path::{Path, PathBuf};
7777
use std::sync::atomic::{AtomicBool, Ordering};
7878
use std::sync::{Arc, Mutex};
7979

80-
use gccjit::{CType, Context, OptimizationLevel};
8180
#[cfg(feature = "master")]
82-
use gccjit::{TargetInfo, Version};
81+
use gccjit::TargetInfo;
82+
use gccjit::{CType, Context, OptimizationLevel};
8383
use rustc_ast::expand::allocator::AllocatorMethod;
8484
use rustc_codegen_ssa::back::lto::{SerializedModule, ThinModule};
8585
use rustc_codegen_ssa::back::write::{
@@ -99,7 +99,7 @@ use rustc_middle::util::Providers;
9999
use rustc_session::Session;
100100
use rustc_session::config::{OptLevel, OutputFilenames};
101101
use rustc_span::Symbol;
102-
use rustc_target::spec::{Arch, RelocModel};
102+
use rustc_target::spec::RelocModel;
103103
use tempfile::TempDir;
104104

105105
use crate::back::lto::ModuleBuffer;
@@ -312,27 +312,6 @@ impl CodegenBackend for GccCodegenBackend {
312312
}
313313
}
314314

315-
fn new_context<'gcc, 'tcx>(tcx: TyCtxt<'tcx>) -> Context<'gcc> {
316-
let context = Context::default();
317-
if matches!(tcx.sess.target.arch, Arch::X86 | Arch::X86_64) {
318-
context.add_command_line_option("-masm=intel");
319-
}
320-
#[cfg(feature = "master")]
321-
{
322-
context.set_special_chars_allowed_in_func_names("$.*");
323-
let version = Version::get();
324-
let version = format!("{}.{}.{}", version.major, version.minor, version.patch);
325-
context.set_output_ident(&format!(
326-
"rustc version {} with libgccjit {}",
327-
rustc_interface::util::rustc_version_str().unwrap_or("unknown version"),
328-
version,
329-
));
330-
}
331-
// TODO(antoyo): check if this should only be added when using -Cforce-unwind-tables=n.
332-
context.add_command_line_option("-fno-asynchronous-unwind-tables");
333-
context
334-
}
335-
336315
impl ExtraBackendMethods for GccCodegenBackend {
337316
fn supports_parallel(&self) -> bool {
338317
false
@@ -346,7 +325,7 @@ impl ExtraBackendMethods for GccCodegenBackend {
346325
) -> Self::Module {
347326
let lto_supported = self.lto_supported.load(Ordering::SeqCst);
348327
let mut mods = GccContext {
349-
context: Arc::new(SyncContext::new(new_context(tcx))),
328+
context: Arc::new(SyncContext::new(gcc_util::new_context(tcx.sess))),
350329
relocation_model: tcx.sess.relocation_model(),
351330
lto_mode: LtoMode::None,
352331
lto_supported,

0 commit comments

Comments
 (0)