Path: blob/main/cranelift/codegen/meta/src/cdsl/instructions.rs
1693 views
use std::fmt;1use std::rc::Rc;23use crate::cdsl::camel_case;4use crate::cdsl::formats::InstructionFormat;5use crate::cdsl::operands::Operand;6use crate::cdsl::typevar::TypeVar;78pub(crate) type AllInstructions = Vec<Instruction>;910pub(crate) struct InstructionGroupBuilder<'all_inst> {11all_instructions: &'all_inst mut AllInstructions,12}1314impl<'all_inst> InstructionGroupBuilder<'all_inst> {15pub fn new(all_instructions: &'all_inst mut AllInstructions) -> Self {16Self { all_instructions }17}1819pub fn push(&mut self, builder: InstructionBuilder) {20let inst = builder.build();21self.all_instructions.push(inst);22}23}2425#[derive(Debug)]26pub(crate) struct PolymorphicInfo {27pub use_typevar_operand: bool,28pub ctrl_typevar: TypeVar,29}3031#[derive(Debug)]32pub(crate) struct InstructionContent {33/// Instruction mnemonic, also becomes opcode name.34pub name: String,35pub camel_name: String,3637/// Documentation string.38pub doc: String,3940/// Input operands. This can be a mix of SSA value operands and other operand kinds.41pub operands_in: Vec<Operand>,42/// Output operands. The output operands must be SSA values or `variable_args`.43pub operands_out: Vec<Operand>,4445/// Instruction format.46pub format: Rc<InstructionFormat>,4748/// One of the input or output operands is a free type variable. None if the instruction is not49/// polymorphic, set otherwise.50pub polymorphic_info: Option<PolymorphicInfo>,5152/// Indices in operands_in of input operands that are values.53pub value_opnums: Vec<usize>,54/// Indices in operands_in of input operands that are immediates or entities.55pub imm_opnums: Vec<usize>,56/// Indices in operands_out of output operands that are values.57pub value_results: Vec<usize>,5859/// True for instructions that terminate the block.60pub is_terminator: bool,61/// True for all branch or jump instructions.62pub is_branch: bool,63/// Is this a call instruction?64pub is_call: bool,65/// Is this a return instruction?66pub is_return: bool,67/// Can this instruction read from memory?68pub can_load: bool,69/// Can this instruction write to memory?70pub can_store: bool,71/// Can this instruction cause a trap?72pub can_trap: bool,73/// Does this instruction have other side effects besides can_* flags?74pub other_side_effects: bool,75/// Despite having other side effects, is this instruction okay to GVN?76pub side_effects_idempotent: bool,77}7879impl InstructionContent {80pub fn snake_name(&self) -> &str {81if &self.name == "return" {82"return_"83} else {84&self.name85}86}87}8889pub(crate) type Instruction = Rc<InstructionContent>;9091impl fmt::Display for InstructionContent {92fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {93if !self.operands_out.is_empty() {94let operands_out = self95.operands_out96.iter()97.map(|op| op.name)98.collect::<Vec<_>>()99.join(", ");100fmt.write_str(&operands_out)?;101fmt.write_str(" = ")?;102}103104fmt.write_str(&self.name)?;105106if !self.operands_in.is_empty() {107let operands_in = self108.operands_in109.iter()110.map(|op| op.name)111.collect::<Vec<_>>()112.join(", ");113fmt.write_str(" ")?;114fmt.write_str(&operands_in)?;115}116117Ok(())118}119}120121pub(crate) struct InstructionBuilder {122name: String,123doc: String,124format: Rc<InstructionFormat>,125operands_in: Option<Vec<Operand>>,126operands_out: Option<Vec<Operand>>,127128// See Instruction comments for the meaning of these fields.129is_terminator: bool,130is_branch: bool,131is_call: bool,132is_return: bool,133can_load: bool,134can_store: bool,135can_trap: bool,136other_side_effects: bool,137side_effects_idempotent: bool,138}139140impl InstructionBuilder {141pub fn new<S: Into<String>>(name: S, doc: S, format: &Rc<InstructionFormat>) -> Self {142Self {143name: name.into(),144doc: doc.into(),145format: format.clone(),146operands_in: None,147operands_out: None,148149is_terminator: false,150is_branch: false,151is_call: false,152is_return: false,153can_load: false,154can_store: false,155can_trap: false,156other_side_effects: false,157side_effects_idempotent: false,158}159}160161pub fn operands_in(mut self, operands: Vec<Operand>) -> Self {162assert!(self.operands_in.is_none());163self.operands_in = Some(operands);164self165}166167pub fn operands_out(mut self, operands: Vec<Operand>) -> Self {168assert!(self.operands_out.is_none());169self.operands_out = Some(operands);170self171}172173/// Mark this instruction as a block terminator.174pub fn terminates_block(mut self) -> Self {175self.is_terminator = true;176self177}178179/// Mark this instruction as a branch instruction. This also implies that the instruction is a180/// block terminator.181pub fn branches(mut self) -> Self {182self.is_branch = true;183self.terminates_block()184}185186/// Mark this instruction as a call instruction.187pub fn call(mut self) -> Self {188self.is_call = true;189self190}191192/// Mark this instruction as a return instruction. This also implies that the instruction is a193/// block terminator.194pub fn returns(mut self) -> Self {195self.is_return = true;196self.terminates_block()197}198199/// Mark this instruction as one that can load from memory.200pub fn can_load(mut self) -> Self {201self.can_load = true;202self203}204205/// Mark this instruction as one that can store to memory.206pub fn can_store(mut self) -> Self {207self.can_store = true;208self209}210211/// Mark this instruction as possibly trapping.212pub fn can_trap(mut self) -> Self {213self.can_trap = true;214self215}216217/// Mark this instruction as one that has side-effects.218pub fn other_side_effects(mut self) -> Self {219self.other_side_effects = true;220self221}222223/// Mark this instruction as one whose side-effects may be de-duplicated.224pub fn side_effects_idempotent(mut self) -> Self {225self.side_effects_idempotent = true;226self227}228229fn build(self) -> Instruction {230let operands_in = self.operands_in.unwrap_or_default();231let operands_out = self.operands_out.unwrap_or_default();232233let mut value_opnums = Vec::new();234let mut imm_opnums = Vec::new();235for (i, op) in operands_in.iter().enumerate() {236if op.is_value() {237value_opnums.push(i);238} else if op.is_immediate_or_entityref() {239imm_opnums.push(i);240} else {241assert!(op.is_varargs());242}243}244245let value_results = operands_out246.iter()247.enumerate()248.filter_map(|(i, op)| if op.is_value() { Some(i) } else { None })249.collect();250251verify_format(&self.name, &operands_in, &self.format);252253let polymorphic_info =254verify_polymorphic(&operands_in, &operands_out, &self.format, &value_opnums);255256let camel_name = camel_case(&self.name);257258Rc::new(InstructionContent {259name: self.name,260camel_name,261doc: self.doc,262operands_in,263operands_out,264format: self.format,265polymorphic_info,266value_opnums,267value_results,268imm_opnums,269is_terminator: self.is_terminator,270is_branch: self.is_branch,271is_call: self.is_call,272is_return: self.is_return,273can_load: self.can_load,274can_store: self.can_store,275can_trap: self.can_trap,276other_side_effects: self.other_side_effects,277side_effects_idempotent: self.side_effects_idempotent,278})279}280}281282/// Checks that the input operands actually match the given format.283fn verify_format(inst_name: &str, operands_in: &[Operand], format: &InstructionFormat) {284// A format is defined by:285// - its number of input value operands,286// - its number and names of input immediate operands,287// - whether it has a value list or not.288let mut num_values = 0;289let mut num_blocks = 0;290let mut num_raw_blocks = 0;291let mut num_immediates = 0;292293for operand in operands_in.iter() {294if operand.is_varargs() {295assert!(296format.has_value_list,297"instruction {} has varargs, but its format {} doesn't have a value list; you may \298need to use a different format.",299inst_name, format.name300);301}302if operand.is_value() {303num_values += 1;304}305if operand.kind.is_block() {306num_blocks += 1;307} else if operand.kind.is_raw_block() {308num_raw_blocks += 1;309} else if operand.is_immediate_or_entityref() {310if let Some(format_field) = format.imm_fields.get(num_immediates) {311assert_eq!(312format_field.kind.rust_field_name,313operand.kind.rust_field_name,314"{}th operand of {} should be {} (according to format), not {} (according to \315inst definition). You may need to use a different format.",316num_immediates,317inst_name,318format_field.kind.rust_field_name,319operand.kind.rust_field_name320);321num_immediates += 1;322}323}324}325326assert_eq!(327num_values, format.num_value_operands,328"inst {} doesn't have as many value input operands as its format {} declares; you may need \329to use a different format.",330inst_name, format.name331);332333assert_eq!(334num_blocks, format.num_block_operands,335"inst {} doesn't have as many block input operands as its format {} declares; you may need \336to use a different format.",337inst_name, format.name,338);339340assert_eq!(341num_raw_blocks, format.num_raw_block_operands,342"inst {} doesn't have as many raw-block input operands as its format {} declares; you may need \343to use a different format.",344inst_name, format.name,345);346347assert_eq!(348num_immediates,349format.imm_fields.len(),350"inst {} doesn't have as many immediate input \351operands as its format {} declares; you may need to use a different format.",352inst_name,353format.name354);355}356357/// Check if this instruction is polymorphic, and verify its use of type variables.358fn verify_polymorphic(359operands_in: &[Operand],360operands_out: &[Operand],361format: &InstructionFormat,362value_opnums: &[usize],363) -> Option<PolymorphicInfo> {364// The instruction is polymorphic if it has one free input or output operand.365let is_polymorphic = operands_in366.iter()367.any(|op| op.is_value() && op.type_var().unwrap().free_typevar().is_some())368|| operands_out369.iter()370.any(|op| op.is_value() && op.type_var().unwrap().free_typevar().is_some());371372if !is_polymorphic {373return None;374}375376// Verify the use of type variables.377let tv_op = format.typevar_operand;378let mut maybe_error_message = None;379if let Some(tv_op) = tv_op {380if tv_op < value_opnums.len() {381let op_num = value_opnums[tv_op];382let tv = operands_in[op_num].type_var().unwrap();383let free_typevar = tv.free_typevar();384if (free_typevar.is_some() && tv == &free_typevar.unwrap())385|| tv.singleton_type().is_some()386{387match is_ctrl_typevar_candidate(tv, operands_in, operands_out) {388Ok(_other_typevars) => {389return Some(PolymorphicInfo {390use_typevar_operand: true,391ctrl_typevar: tv.clone(),392});393}394Err(error_message) => {395maybe_error_message = Some(error_message);396}397}398}399}400};401402// If we reached here, it means the type variable indicated as the typevar operand couldn't403// control every other input and output type variable. We need to look at the result type404// variables.405if operands_out.is_empty() {406// No result means no other possible type variable, so it's a type inference failure.407match maybe_error_message {408Some(msg) => panic!("{}", msg),409None => panic!("typevar_operand must be a free type variable"),410}411}412413// Otherwise, try to infer the controlling type variable by looking at the first result.414let tv = operands_out[0].type_var().unwrap();415let free_typevar = tv.free_typevar();416if free_typevar.is_some() && tv != &free_typevar.unwrap() {417panic!("first result must be a free type variable");418}419420// At this point, if the next unwrap() fails, it means the output type couldn't be used as a421// controlling type variable either; panicking is the right behavior.422is_ctrl_typevar_candidate(tv, operands_in, operands_out).unwrap();423424Some(PolymorphicInfo {425use_typevar_operand: false,426ctrl_typevar: tv.clone(),427})428}429430/// Verify that the use of TypeVars is consistent with `ctrl_typevar` as the controlling type431/// variable.432///433/// All polymorphic inputs must either be derived from `ctrl_typevar` or be independent free type434/// variables only used once.435///436/// All polymorphic results must be derived from `ctrl_typevar`.437///438/// Return a vector of other type variables used, or a string explaining what went wrong.439fn is_ctrl_typevar_candidate(440ctrl_typevar: &TypeVar,441operands_in: &[Operand],442operands_out: &[Operand],443) -> Result<Vec<TypeVar>, String> {444let mut other_typevars = Vec::new();445446// Check value inputs.447for input in operands_in {448if !input.is_value() {449continue;450}451452let typ = input.type_var().unwrap();453let free_typevar = typ.free_typevar();454455// Non-polymorphic or derived from ctrl_typevar is OK.456if free_typevar.is_none() {457continue;458}459let free_typevar = free_typevar.unwrap();460if &free_typevar == ctrl_typevar {461continue;462}463464// No other derived typevars allowed.465if typ != &free_typevar {466return Err(format!(467"{:?}: type variable {} must be derived from {:?} while it is derived from {:?}",468input, typ.name, ctrl_typevar, free_typevar469));470}471472// Other free type variables can only be used once each.473for other_tv in &other_typevars {474if &free_typevar == other_tv {475return Err(format!(476"non-controlling type variable {} can't be used more than once",477free_typevar.name478));479}480}481482other_typevars.push(free_typevar);483}484485// Check outputs.486for result in operands_out {487if !result.is_value() {488continue;489}490491let typ = result.type_var().unwrap();492let free_typevar = typ.free_typevar();493494// Non-polymorphic or derived from ctrl_typevar is OK.495if free_typevar.is_none() || &free_typevar.unwrap() == ctrl_typevar {496continue;497}498499return Err("type variable in output not derived from ctrl_typevar".into());500}501502Ok(other_typevars)503}504505506