Skip to content

Commit

Permalink
Catch operation build failure (#353)
Browse files Browse the repository at this point in the history
Close #275.
  • Loading branch information
raviqqe authored Oct 18, 2023
1 parent fdb233e commit f14e2a5
Show file tree
Hide file tree
Showing 17 changed files with 177 additions and 53 deletions.
2 changes: 1 addition & 1 deletion macro/src/dialect/operation/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl<'o> OperationBuilder<'o> {
quote! {
impl<'c> #builder_ident<'c, #(#arguments),*> {
pub fn build(self) -> #class_name<'c> {
self.builder #maybe_infer.build().try_into().expect(#error)
self.builder #maybe_infer.build().expect("valid operation").try_into().expect(#error)
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions macro/src/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub fn generate_binary(dialect: &Ident, names: &[Ident]) -> Result<TokenStream,
.add_operands(&[lhs, rhs])
.enable_result_type_inference()
.build()
.expect("valid operation")
}
}));

Expand Down Expand Up @@ -71,6 +72,7 @@ pub fn generate_unary(dialect: &Ident, names: &[Ident]) -> Result<TokenStream, B
.add_operands(&[value])
.enable_result_type_inference()
.build()
.expect("valid operation")
}
}));

Expand Down Expand Up @@ -112,6 +114,7 @@ pub fn generate_typed_unary(
.add_operands(&[value])
.add_results(&[r#type])
.build()
.expect("valid operation")
}
}));

Expand Down
3 changes: 3 additions & 0 deletions melior/src/dialect/arith.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub fn constant<'c>(
.add_attributes(&[(Identifier::new(context, "value"), value)])
.enable_result_type_inference()
.build()
.expect("valid operation")
}

/// `arith.cmpf` predicate
Expand Down Expand Up @@ -94,6 +95,7 @@ fn cmp<'c>(
.add_operands(&[lhs, rhs])
.enable_result_type_inference()
.build()
.expect("valid operation")
}

/// Creates an `arith.select` operation.
Expand All @@ -108,6 +110,7 @@ pub fn select<'c>(
.add_operands(&[condition, true_value, false_value])
.add_results(&[true_value.r#type()])
.build()
.expect("valid operation")
}

melior_macro::binary_operations!(
Expand Down
6 changes: 5 additions & 1 deletion melior/src/dialect/cf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub fn assert<'c>(
)])
.add_operands(&[argument])
.build()
.expect("valid operation")
}

/// Creates a `cf.br` operation.
Expand All @@ -39,6 +40,7 @@ pub fn br<'c>(
.add_operands(destination_operands)
.add_successors(&[successor])
.build()
.expect("valid operation")
}

/// Creates a `cf.cond_br` operation.
Expand Down Expand Up @@ -73,6 +75,7 @@ pub fn cond_br<'c>(
)
.add_successors(&[true_successor, false_successor])
.build()
.expect("valid operation")
}

/// Creates a `cf.switch` operation.
Expand Down Expand Up @@ -137,7 +140,8 @@ pub fn switch<'c>(
.collect::<Vec<_>>(),
)
.add_successors(&destinations)
.build())
.build()
.expect("valid operation"))
}

#[cfg(test)]
Expand Down
5 changes: 5 additions & 0 deletions melior/src/dialect/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub fn call<'c>(
.add_operands(arguments)
.add_results(result_types)
.build()
.expect("valid operation")
}

/// Create a `func.call_indirect` operation.
Expand All @@ -38,6 +39,7 @@ pub fn call_indirect<'c>(
.add_operands(arguments)
.add_results(result_types)
.build()
.expect("valid operation")
}

/// Create a `func.constant` operation.
Expand All @@ -51,6 +53,7 @@ pub fn constant<'c>(
.add_attributes(&[(Identifier::new(context, "value"), function.into())])
.add_results(&[r#type.into()])
.build()
.expect("valid operation")
}

/// Create a `func.func` operation.
Expand All @@ -70,6 +73,7 @@ pub fn func<'c>(
.add_attributes(attributes)
.add_regions(vec![region])
.build()
.expect("valid operation")
}

/// Create a `func.return` operation.
Expand All @@ -81,6 +85,7 @@ pub fn r#return<'c>(
OperationBuilder::new(context, "func.return", location)
.add_operands(operands)
.build()
.expect("valid operation")
}

#[cfg(test)]
Expand Down
2 changes: 2 additions & 0 deletions melior/src/dialect/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub fn constant<'c>(
.add_attributes(&[(Identifier::new(context, "value"), value.into())])
.enable_result_type_inference()
.build()
.expect("valid operation")
}

/// Creates an `index.cmp` operation.
Expand Down Expand Up @@ -54,6 +55,7 @@ pub fn cmp<'c>(
.add_operands(&[lhs, rhs])
.enable_result_type_inference()
.build()
.expect("valid operation")
}

melior_macro::binary_operations!(
Expand Down
26 changes: 24 additions & 2 deletions melior/src/dialect/llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub fn extract_value<'c>(
.add_operands(&[container])
.add_results(&[result_type])
.build()
.expect("valid operation")
}

/// Creates a `llvm.getelementptr` operation.
Expand All @@ -60,6 +61,7 @@ pub fn get_element_ptr<'c>(
.add_operands(&[ptr])
.add_results(&[result_type])
.build()
.expect("valid operation")
}

/// Creates a `llvm.getelementptr` operation with dynamic indices.
Expand All @@ -86,6 +88,7 @@ pub fn get_element_ptr_dynamic<'c, const N: usize>(
.add_operands(indices)
.add_results(&[result_type])
.build()
.expect("valid operation")
}

/// Creates a `llvm.insertvalue` operation.
Expand All @@ -101,6 +104,7 @@ pub fn insert_value<'c>(
.add_operands(&[container, value])
.enable_result_type_inference()
.build()
.expect("valid operation")
}

/// Creates a `llvm.mlir.undef` operation.
Expand All @@ -112,6 +116,7 @@ pub fn undef<'c>(
OperationBuilder::new(context, "llvm.mlir.undef", location)
.add_results(&[result_type])
.build()
.expect("valid operation")
}

/// Creates a `llvm.mlir.poison` operation.
Expand All @@ -123,6 +128,7 @@ pub fn poison<'c>(
OperationBuilder::new(context, "llvm.mlir.poison", location)
.add_results(&[result_type])
.build()
.expect("valid operation")
}

/// Creates a `llvm.mlir.null` operation. A null pointer.
Expand All @@ -134,11 +140,14 @@ pub fn nullptr<'c>(
OperationBuilder::new(context, "llvm.mlir.null", location)
.add_results(&[ptr_type])
.build()
.expect("valid operation")
}

/// Creates a `llvm.unreachable` operation.
pub fn unreachable<'c>(context: &'c Context, location: Location<'c>) -> Operation<'c> {
OperationBuilder::new(context, "llvm.unreachable", location).build()
OperationBuilder::new(context, "llvm.unreachable", location)
.build()
.expect("valid operation")
}

/// Creates a `llvm.bitcast` operation.
Expand All @@ -152,6 +161,7 @@ pub fn bitcast<'c>(
.add_operands(&[argument])
.add_results(&[result])
.build()
.expect("valid operation")
}

/// Creates a `llvm.alloca` operation.
Expand All @@ -167,6 +177,7 @@ pub fn alloca<'c>(
.add_attributes(&extra_options.into_attributes(context))
.add_results(&[ptr_type])
.build()
.expect("valid operation")
}

/// Creates a `llvm.store` operation.
Expand All @@ -181,6 +192,7 @@ pub fn store<'c>(
.add_operands(&[value, addr])
.add_attributes(&extra_options.into_attributes(context))
.build()
.expect("valid operation")
}

/// Creates a `llvm.load` operation.
Expand All @@ -196,6 +208,7 @@ pub fn load<'c>(
.add_attributes(&extra_options.into_attributes(context))
.add_results(&[r#type])
.build()
.expect("valid operation")
}

/// Create a `llvm.func` operation.
Expand All @@ -215,6 +228,7 @@ pub fn func<'c>(
.add_attributes(attributes)
.add_regions(vec![region])
.build()
.expect("valid operation")
}

// Creates a `llvm.return` operation.
Expand All @@ -229,7 +243,7 @@ pub fn r#return<'c>(
builder = builder.add_operands(&[value]);
}

builder.build()
builder.build().expect("valid operation")
}

/// Creates a `llvm.call_intrinsic` operation.
Expand All @@ -245,6 +259,7 @@ pub fn call_intrinsic<'c>(
.add_attributes(&[(Identifier::new(context, "intrin"), intrin.into())])
.add_results(results)
.build()
.expect("valid operation")
}

/// Creates a `llvm.intr.ctlz` operation.
Expand All @@ -264,6 +279,7 @@ pub fn intr_ctlz<'c>(
.add_operands(&[value])
.add_results(&[result_type])
.build()
.expect("valid operation")
}

/// Creates a `llvm.intr.ctlz` operation.
Expand All @@ -283,6 +299,7 @@ pub fn intr_cttz<'c>(
.add_operands(&[value])
.add_results(&[result_type])
.build()
.expect("valid operation")
}

/// Creates a `llvm.intr.ctlz` operation.
Expand All @@ -296,6 +313,7 @@ pub fn intr_ctpop<'c>(
.add_operands(&[value])
.add_results(&[result_type])
.build()
.expect("valid operation")
}

/// Creates a `llvm.intr.bswap` operation.
Expand All @@ -309,6 +327,7 @@ pub fn intr_bswap<'c>(
.add_operands(&[value])
.add_results(&[result_type])
.build()
.expect("valid operation")
}

/// Creates a `llvm.intr.bitreverse` operation.
Expand All @@ -322,6 +341,7 @@ pub fn intr_bitreverse<'c>(
.add_operands(&[value])
.add_results(&[result_type])
.build()
.expect("valid operation")
}

/// Creates a `llvm.intr.abs` operation.
Expand All @@ -344,6 +364,7 @@ pub fn intr_abs<'c>(
.add_operands(&[value])
.add_results(&[result_type])
.build()
.expect("valid operation")
}

/// Creates a `llvm.zext` operation.
Expand All @@ -357,6 +378,7 @@ pub fn zext<'c>(
.add_operands(&[value])
.add_results(&[result_type])
.build()
.expect("valid operation")
}

#[cfg(test)]
Expand Down
Loading

0 comments on commit f14e2a5

Please sign in to comment.