Skip to content

Commit 7be6ee1

Browse files
authored
Merge pull request #868 from bjorn3/misc_changes
Move most flags from module_codegen to new_context
2 parents d189e9f + 1ab95d4 commit 7be6ee1

4 files changed

Lines changed: 143 additions & 150 deletions

File tree

src/back/write.rs

Lines changed: 1 addition & 4 deletions
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(
@@ -68,9 +68,6 @@ pub(crate) fn codegen(
6868
let _timer = prof
6969
.generic_activity_with_arg("GCC_module_codegen_embed_bitcode", &*module.name);
7070
if lto_supported {
71-
// FIXME(antoyo): maybe we should call embed_bitcode to have the proper iOS fixes?
72-
//embed_bitcode(cgcx, llcx, llmod, &config.bc_cmdline, data);
73-
7471
context.add_command_line_option("-flto=auto");
7572
context.add_command_line_option("-flto-partition=one");
7673
context.add_command_line_option("-ffat-lto-objects");

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

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

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

@@ -270,24 +176,3 @@ pub fn compile_codegen_unit(
270176

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

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+
// FIXME(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+
// base (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::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-
// FIXME(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)