Skip to content
This repository was archived by the owner on Jun 26, 2020. It is now read-only.

Generalize "global values" to "templates". #1299

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cranelift-codegen/meta/src/cdsl/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1152,10 +1152,10 @@ impl InstructionPredicate {
}

pub fn new_is_colocated_data(formats: &Formats) -> InstructionPredicateNode {
let format = &formats.unary_global_value;
let format = &formats.unary_template;
InstructionPredicateNode::FormatPredicate(FormatPredicateNode::new(
&*format,
"global_value",
"template",
FormatPredicateKind::IsColocatedData,
))
}
Expand Down
16 changes: 8 additions & 8 deletions cranelift-codegen/meta/src/isa/x86/recipes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1211,13 +1211,13 @@ pub(crate) fn define<'shared>(

// XX+rd id with Abs4 globalsym relocation.
recipes.add_template_recipe(
EncodingRecipeBuilder::new("gvaddr4", &formats.unary_global_value, 4)
EncodingRecipeBuilder::new("gvaddr4", &formats.unary_template, 4)
.operands_out(vec![gpr])
.emit(
r#"
{{PUT_OP}}(bits | (out_reg0 & 7), rex1(out_reg0), sink);
sink.reloc_external(Reloc::Abs4,
&func.global_values[global_value].symbol_name(),
&func.templates[template].symbol_name(),
0);
sink.put4(0);
"#,
Expand All @@ -1226,13 +1226,13 @@ pub(crate) fn define<'shared>(

// XX+rd iq with Abs8 globalsym relocation.
recipes.add_template_recipe(
EncodingRecipeBuilder::new("gvaddr8", &formats.unary_global_value, 8)
EncodingRecipeBuilder::new("gvaddr8", &formats.unary_template, 8)
.operands_out(vec![gpr])
.emit(
r#"
{{PUT_OP}}(bits | (out_reg0 & 7), rex1(out_reg0), sink);
sink.reloc_external(Reloc::Abs8,
&func.global_values[global_value].symbol_name(),
&func.templates[template].symbol_name(),
0);
sink.put8(0);
"#,
Expand All @@ -1241,7 +1241,7 @@ pub(crate) fn define<'shared>(

// XX+rd iq with PCRel4 globalsym relocation.
recipes.add_template_recipe(
EncodingRecipeBuilder::new("pcrel_gvaddr8", &formats.unary_global_value, 5)
EncodingRecipeBuilder::new("pcrel_gvaddr8", &formats.unary_template, 5)
.operands_out(vec![gpr])
.emit(
r#"
Expand All @@ -1250,7 +1250,7 @@ pub(crate) fn define<'shared>(
// The addend adjusts for the difference between the end of the
// instruction and the beginning of the immediate field.
sink.reloc_external(Reloc::X86PCRel4,
&func.global_values[global_value].symbol_name(),
&func.templates[template].symbol_name(),
-4);
sink.put4(0);
"#,
Expand All @@ -1259,7 +1259,7 @@ pub(crate) fn define<'shared>(

// XX+rd iq with Abs8 globalsym relocation.
recipes.add_template_recipe(
EncodingRecipeBuilder::new("got_gvaddr8", &formats.unary_global_value, 5)
EncodingRecipeBuilder::new("got_gvaddr8", &formats.unary_template, 5)
.operands_out(vec![gpr])
.emit(
r#"
Expand All @@ -1268,7 +1268,7 @@ pub(crate) fn define<'shared>(
// The addend adjusts for the difference between the end of the
// instruction and the beginning of the immediate field.
sink.reloc_external(Reloc::X86GOTPCRel4,
&func.global_values[global_value].symbol_name(),
&func.templates[template].symbol_name(),
-4);
sink.put4(0);
"#,
Expand Down
6 changes: 3 additions & 3 deletions cranelift-codegen/meta/src/shared/entities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ pub(crate) struct EntityRefs {
/// A reference to a stack slot declared in the function preamble.
pub(crate) stack_slot: OperandKind,

/// A reference to a global value.
pub(crate) global_value: OperandKind,
/// A reference to a template.
pub(crate) template: OperandKind,

/// A reference to a function signature declared in the function preamble.
/// This is used to provide the call signature in a call_indirect instruction.
Expand Down Expand Up @@ -47,7 +47,7 @@ impl EntityRefs {
),
stack_slot: new("stack_slot", "ir::StackSlot", "A stack slot"),

global_value: new("global_value", "ir::GlobalValue", "A global value."),
template: new("template", "ir::Template", "A template."),

sig_ref: new("sig_ref", "ir::SigRef", "A function signature."),

Expand Down
6 changes: 3 additions & 3 deletions cranelift-codegen/meta/src/shared/formats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub(crate) struct Formats {
pub(crate) unary: Rc<InstructionFormat>,
pub(crate) unary_bool: Rc<InstructionFormat>,
pub(crate) unary_const: Rc<InstructionFormat>,
pub(crate) unary_global_value: Rc<InstructionFormat>,
pub(crate) unary_template: Rc<InstructionFormat>,
pub(crate) unary_ieee32: Rc<InstructionFormat>,
pub(crate) unary_ieee64: Rc<InstructionFormat>,
pub(crate) unary_imm: Rc<InstructionFormat>,
Expand All @@ -70,8 +70,8 @@ impl Formats {

unary_const: Builder::new("UnaryConst").imm(&imm.pool_constant).build(),

unary_global_value: Builder::new("UnaryGlobalValue")
.imm(&entities.global_value)
unary_template: Builder::new("UnaryTemplate")
.imm(&entities.template)
.build(),

binary: Builder::new("Binary").value().value().build(),
Expand Down
16 changes: 8 additions & 8 deletions cranelift-codegen/meta/src/shared/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -953,29 +953,29 @@ pub(crate) fn define(
.operands_out(vec![addr]),
);

let GV = &Operand::new("GV", &entities.global_value);
let TEMPLATE = &Operand::new("TEMPLATE", &entities.template);

ig.push(
Inst::new(
"global_value",
"template",
r#"
Compute the value of global GV.
Expand a template to compute a value.
"#,
&formats.unary_global_value,
&formats.unary_template,
)
.operands_in(vec![GV])
.operands_in(vec![TEMPLATE])
.operands_out(vec![a]),
);

ig.push(
Inst::new(
"symbol_value",
r#"
Compute the value of global GV, which is a symbolic value.
Compute the address of a symbol"
"#,
&formats.unary_global_value,
&formats.unary_template,
)
.operands_in(vec![GV])
.operands_in(vec![TEMPLATE])
.operands_out(vec![a]),
);

Expand Down
2 changes: 1 addition & 1 deletion cranelift-codegen/meta/src/shared/legalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ pub(crate) fn define(insts: &InstructionGroup, imm: &Immediates) -> TransformGro
let trapz = insts.by_name("trapz");

// Custom expansions for memory objects.
expand.custom_legalize(insts.by_name("global_value"), "expand_global_value");
expand.custom_legalize(insts.by_name("template"), "expand_template");
expand.custom_legalize(insts.by_name("heap_addr"), "expand_heap_addr");
expand.custom_legalize(insts.by_name("table_addr"), "expand_table_addr");

Expand Down
4 changes: 2 additions & 2 deletions cranelift-codegen/meta/src/shared/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub(crate) fn define() -> SettingGroup {

// Note that Cranelift doesn't currently need an is_pie flag, because PIE is
// just PIC where symbols can't be pre-empted, which can be expressed with the
// `colocated` flag on external functions and global values.
// `colocated` flag on external functions and templates.
settings.add_bool(
"is_pic",
"Enable Position-Independent Code generation",
Expand Down Expand Up @@ -102,7 +102,7 @@ pub(crate) fn define() -> SettingGroup {

Enabling this requires the enable_pinned_reg setting to be set to true. It enables a custom
legalization of the `heap_addr` instruction so it will use the pinned register as the heap
base, instead of fetching it from a global value.
base, instead of computing it with a template.

Warning! Enabling this means that the pinned register *must* be maintained to contain the
heap base address at all times, during the lifetime of a function. Using the pinned
Expand Down
38 changes: 19 additions & 19 deletions cranelift-codegen/src/ir/entities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,29 +125,29 @@ impl StackSlot {
}
}

/// An opaque reference to a global value.
/// An opaque reference to a template.
///
/// A `GlobalValue` is a [`Value`](Value) that will be live across the entire
/// function lifetime. It can be preloaded from other global values.
/// A `Template` is an expression that can be expanded into code or interpreted
/// directly.
///
/// You can create a `GlobalValue` in the following ways:
/// You can create a `Template` in the following ways:
///
/// - When compiling to WASM, you can use it to load values from a
/// [`VmContext`](super::GlobalValueData::VMContext) using
/// [`FuncEnvironment::make_global`](https://docs.rs/cranelift-wasm/*/cranelift_wasm/trait.FuncEnvironment.html#tymethod.make_global).
/// [`VmContext`](super::TemplateData::VMContext) using
/// [`FuncEnvironment::make_template`](https://docs.rs/cranelift-wasm/*/cranelift_wasm/trait.FuncEnvironment.html#tymethod.make_template).
/// - When compiling to native code, you can use it for objects in static memory with
/// [`Module::declare_data_in_func`](https://docs.rs/cranelift-module/*/cranelift_module/struct.Module.html#method.declare_data_in_func).
/// - For any compilation target, it can be registered with
/// [`FunctionBuilder::create_global_value`](https://docs.rs/cranelift-frontend/*/cranelift_frontend/struct.FunctionBuilder.html#method.create_global_value).
/// [`FunctionBuilder::create_template`](https://docs.rs/cranelift-frontend/*/cranelift_frontend/struct.FunctionBuilder.html#method.create_template).
///
/// `GlobalValue`s can be retrieved with
/// [`InstBuilder:global_value`](super::InstBuilder::global_value).
/// `Template`s can be retrieved with
/// [`InstBuilder:template`](super::InstBuilder::template).
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct GlobalValue(u32);
entity_impl!(GlobalValue, "gv");
pub struct Template(u32);
entity_impl!(Template, "template");

impl GlobalValue {
/// Create a new global value reference from its number.
impl Template {
/// Create a new template reference from its number.
///
/// This method is for use by the parser.
pub fn with_number(n: u32) -> Option<Self> {
Expand Down Expand Up @@ -354,8 +354,8 @@ pub enum AnyEntity {
Value(Value),
/// A stack slot.
StackSlot(StackSlot),
/// A Global value.
GlobalValue(GlobalValue),
/// A template.
Template(Template),
/// A jump table.
JumpTable(JumpTable),
/// An external function.
Expand All @@ -376,7 +376,7 @@ impl fmt::Display for AnyEntity {
Self::Inst(r) => r.fmt(f),
Self::Value(r) => r.fmt(f),
Self::StackSlot(r) => r.fmt(f),
Self::GlobalValue(r) => r.fmt(f),
Self::Template(r) => r.fmt(f),
Self::JumpTable(r) => r.fmt(f),
Self::FuncRef(r) => r.fmt(f),
Self::SigRef(r) => r.fmt(f),
Expand Down Expand Up @@ -416,9 +416,9 @@ impl From<StackSlot> for AnyEntity {
}
}

impl From<GlobalValue> for AnyEntity {
fn from(r: GlobalValue) -> Self {
Self::GlobalValue(r)
impl From<Template> for AnyEntity {
fn from(r: Template) -> Self {
Self::Template(r)
}
}

Expand Down
2 changes: 1 addition & 1 deletion cranelift-codegen/src/ir/extfunc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ pub enum ArgumentPurpose {
/// A VM context pointer.
///
/// This is a pointer to a context struct containing details about the current sandbox. It is
/// used as a base pointer for `vmctx` global values.
/// used as a base pointer for `vmctx` templates.
VMContext,

/// A signature identifier.
Expand Down
23 changes: 14 additions & 9 deletions cranelift-codegen/src/ir/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use crate::entity::{PrimaryMap, SecondaryMap};
use crate::ir;
use crate::ir::{DataFlowGraph, ExternalName, Layout, Signature};
use crate::ir::{
Ebb, ExtFuncData, FuncRef, GlobalValue, GlobalValueData, Heap, HeapData, Inst, JumpTable,
JumpTableData, SigRef, StackSlot, StackSlotData, Table, TableData,
Ebb, ExtFuncData, FuncRef, Heap, HeapData, Inst, JumpTable, JumpTableData, SigRef, StackSlot,
StackSlotData, Table, TableData, Template, TemplateData, Type,
};
use crate::ir::{EbbOffsets, FrameLayout, InstEncodings, SourceLocs, StackSlots, ValueLocations};
use crate::ir::{JumpTableOffsets, JumpTables};
Expand Down Expand Up @@ -41,8 +41,8 @@ pub struct Function {
/// Stack slots allocated in this function.
pub stack_slots: StackSlots,

/// Global values referenced.
pub global_values: PrimaryMap<ir::GlobalValue, ir::GlobalValueData>,
/// Templates defined within the function.
pub templates: PrimaryMap<ir::Template, ir::TemplateData>,

/// Heaps referenced.
pub heaps: PrimaryMap<ir::Heap, ir::HeapData>,
Expand Down Expand Up @@ -109,7 +109,7 @@ impl Function {
signature: sig,
old_signature: None,
stack_slots: StackSlots::new(),
global_values: PrimaryMap::new(),
templates: PrimaryMap::new(),
heaps: PrimaryMap::new(),
tables: PrimaryMap::new(),
jump_tables: PrimaryMap::new(),
Expand All @@ -130,7 +130,7 @@ impl Function {
pub fn clear(&mut self) {
self.signature.clear(CallConv::Fast);
self.stack_slots.clear();
self.global_values.clear();
self.templates.clear();
self.heaps.clear();
self.tables.clear();
self.jump_tables.clear();
Expand Down Expand Up @@ -172,9 +172,9 @@ impl Function {
self.dfg.ext_funcs.push(data)
}

/// Declares a global value accessible to the function.
pub fn create_global_value(&mut self, data: GlobalValueData) -> GlobalValue {
self.global_values.push(data)
/// Declares a template within the function.
pub fn create_template(&mut self, data: TemplateData) -> Template {
self.templates.push(data)
}

/// Declares a heap accessible to the function.
Expand Down Expand Up @@ -297,6 +297,11 @@ impl Function {
// function, assume it may call.
!self.dfg.signatures.is_empty()
}

/// Return the result type of the given template.
pub fn template_result_type(&self, template: Template, isa: &dyn TargetIsa) -> Type {
self.templates[template].result_type(isa, &self.templates)
}
}

/// Additional annotations for function display.
Expand Down
Loading