Skip to content

Commit 7fa06f6

Browse files
committed
Avoid a hir access inside get_static
1 parent 6c5a70d commit 7fa06f6

File tree

1 file changed

+51
-78
lines changed

1 file changed

+51
-78
lines changed

src/consts.rs

+51-78
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
use gccjit::FnAttribute;
33
use gccjit::{Function, GlobalKind, LValue, RValue, ToRValue, Type};
44
use rustc_codegen_ssa::traits::{BaseTypeMethods, ConstMethods, DerivedTypeMethods, StaticMethods};
5-
use rustc_hir as hir;
6-
use rustc_hir::Node;
7-
use rustc_middle::{bug, span_bug};
5+
use rustc_middle::span_bug;
86
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
97
use rustc_middle::mir::mono::MonoItem;
108
use rustc_middle::ty::{self, Instance, Ty};
@@ -217,84 +215,59 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
217215
let ty = instance.ty(self.tcx, ty::ParamEnv::reveal_all());
218216
let sym = self.tcx.symbol_name(instance).name;
219217

220-
let global =
221-
if let Some(def_id) = def_id.as_local() {
222-
let id = self.tcx.hir().local_def_id_to_hir_id(def_id);
223-
let llty = self.layout_of(ty).gcc_type(self);
224-
// FIXME: refactor this to work without accessing the HIR
225-
let global = match self.tcx.hir().get(id) {
226-
Node::Item(&hir::Item { span, kind: hir::ItemKind::Static(..), .. }) => {
227-
if let Some(global) = self.get_declared_value(&sym) {
228-
if self.val_ty(global) != self.type_ptr_to(llty) {
229-
span_bug!(span, "Conflicting types for static");
230-
}
231-
}
232-
233-
let is_tls = fn_attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL);
234-
let global = self.declare_global(
235-
&sym,
236-
llty,
237-
GlobalKind::Exported,
238-
is_tls,
239-
fn_attrs.link_section,
240-
);
241-
242-
if !self.tcx.is_reachable_non_generic(def_id) {
243-
// TODO(antoyo): set visibility.
244-
}
245-
246-
global
247-
}
248-
249-
Node::ForeignItem(&hir::ForeignItem {
250-
span,
251-
kind: hir::ForeignItemKind::Static(..),
252-
..
253-
}) => {
254-
let fn_attrs = self.tcx.codegen_fn_attrs(def_id);
255-
check_and_apply_linkage(&self, &fn_attrs, ty, sym, span)
256-
}
257-
258-
item => bug!("get_static: expected static, found {:?}", item),
259-
};
218+
let global = if def_id.is_local() && !self.tcx.is_foreign_item(def_id) {
219+
let llty = self.layout_of(ty).gcc_type(self);
220+
if let Some(global) = self.get_declared_value(sym) {
221+
if self.val_ty(global) != self.type_ptr_to(llty) {
222+
span_bug!(self.tcx.def_span(def_id), "Conflicting types for static");
223+
}
224+
}
260225

261-
global
226+
let is_tls = fn_attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL);
227+
let global = self.declare_global(
228+
&sym,
229+
llty,
230+
GlobalKind::Exported,
231+
is_tls,
232+
fn_attrs.link_section,
233+
);
234+
235+
if !self.tcx.is_reachable_non_generic(def_id) {
236+
// TODO(antoyo): set visibility.
262237
}
263-
else {
264-
// FIXME(nagisa): perhaps the map of externs could be offloaded to llvm somehow?
265-
//debug!("get_static: sym={} item_attr={:?}", sym, self.tcx.item_attrs(def_id));
266-
267-
let attrs = self.tcx.codegen_fn_attrs(def_id);
268-
let span = self.tcx.def_span(def_id);
269-
let global = check_and_apply_linkage(&self, &attrs, ty, sym, span);
270-
271-
let needs_dll_storage_attr = false; // TODO(antoyo)
272-
273-
// If this assertion triggers, there's something wrong with commandline
274-
// argument validation.
275-
debug_assert!(
276-
!(self.tcx.sess.opts.cg.linker_plugin_lto.enabled()
277-
&& self.tcx.sess.target.options.is_like_msvc
278-
&& self.tcx.sess.opts.cg.prefer_dynamic)
279-
);
280-
281-
if needs_dll_storage_attr {
282-
// This item is external but not foreign, i.e., it originates from an external Rust
283-
// crate. Since we don't know whether this crate will be linked dynamically or
284-
// statically in the final application, we always mark such symbols as 'dllimport'.
285-
// If final linkage happens to be static, we rely on compiler-emitted __imp_ stubs
286-
// to make things work.
287-
//
288-
// However, in some scenarios we defer emission of statics to downstream
289-
// crates, so there are cases where a static with an upstream DefId
290-
// is actually present in the current crate. We can find out via the
291-
// is_codegened_item query.
292-
if !self.tcx.is_codegened_item(def_id) {
293-
unimplemented!();
294-
}
238+
239+
global
240+
} else {
241+
check_and_apply_linkage(&self, &fn_attrs, ty, sym, self.tcx.def_span(def_id))
242+
};
243+
244+
if !def_id.is_local() {
245+
let needs_dll_storage_attr = false; // TODO(antoyo)
246+
247+
// If this assertion triggers, there's something wrong with commandline
248+
// argument validation.
249+
debug_assert!(
250+
!(self.tcx.sess.opts.cg.linker_plugin_lto.enabled()
251+
&& self.tcx.sess.target.options.is_like_msvc
252+
&& self.tcx.sess.opts.cg.prefer_dynamic)
253+
);
254+
255+
if needs_dll_storage_attr {
256+
// This item is external but not foreign, i.e., it originates from an external Rust
257+
// crate. Since we don't know whether this crate will be linked dynamically or
258+
// statically in the final application, we always mark such symbols as 'dllimport'.
259+
// If final linkage happens to be static, we rely on compiler-emitted __imp_ stubs
260+
// to make things work.
261+
//
262+
// However, in some scenarios we defer emission of statics to downstream
263+
// crates, so there are cases where a static with an upstream DefId
264+
// is actually present in the current crate. We can find out via the
265+
// is_codegened_item query.
266+
if !self.tcx.is_codegened_item(def_id) {
267+
unimplemented!();
295268
}
296-
global
297-
};
269+
}
270+
}
298271

299272
// TODO(antoyo): set dll storage class.
300273

0 commit comments

Comments
 (0)