This commit is contained in:
Edgar 2023-11-25 12:36:56 +01:00
parent 17921065a1
commit bb7c3c7d2f
No known key found for this signature in database
GPG key ID: 70ADAE8F35904387

View file

@ -92,7 +92,8 @@ let context = Context::new();
context.append_dialect_registry(&registry); context.append_dialect_registry(&registry);
context.load_all_available_dialects(); context.load_all_available_dialects();
// A location is a debug location like in LLVM, in MLIR all operations need a location, even if its "unknown". // A location is a debug location like in LLVM, in MLIR all
// operations need a location, even if its "unknown".
let location = Location::unknown(&context); let location = Location::unknown(&context);
// A MLIR module is akin to a LLVM module. // A MLIR module is akin to a LLVM module.
@ -102,16 +103,25 @@ let module = Module::new(location);
let index_type = Type::index(&context); let index_type = Type::index(&context);
// Append a `func::func` operation to the body (a block) of the module. // Append a `func::func` operation to the body (a block) of the module.
// This operation accepts a string attribute, which is the name. A type attribute, which contains a function type in this case. // This operation accepts a string attribute, which is the name.
// Then it accepts a single region, which is where the body of the function will be, this region can have // A type attribute, which contains a function type in this case.
// multiple blocks, which is how you may implement control flow within the function. These blocks each can have more operations. // Then it accepts a single region, which is where the body
// of the function will be, this region can have
// multiple blocks, which is how you may implement
// control flow within the function.
// These blocks each can have more operations.
module.body().append_operation(func::func( module.body().append_operation(func::func(
&context, &context,
StringAttribute::new(&context, "add"), StringAttribute::new(&context, "add"),
TypeAttribute::new(FunctionType::new(&context, &[index_type, index_type], &[index_type]).into()), TypeAttribute::new(
FunctionType::new(&context, &[index_type, index_type], &[index_type]).into()
),
{ {
// The first block within the region, blocks accept arguments // The first block within the region, blocks accept arguments
// In regions with control flow, MLIR leverages this structure to implicitly represent the passage of control-flow dependent values without the complex nuances of PHI nodes in traditional SSA representations. // In regions with control flow, MLIR leverages
// this structure to implicitly represent
// the passage of control-flow dependent values without the complex nuances
// of PHI nodes in traditional SSA representations.
let block = Block::new(&[(index_type, location), (index_type, location)]); let block = Block::new(&[(index_type, location), (index_type, location)]);
// Use the arith dialect to add the 2 arguments. // Use the arith dialect to add the 2 arguments.
@ -122,7 +132,9 @@ module.body().append_operation(func::func(
)); ));
// Return the result using the "func" dialect return operation. // Return the result using the "func" dialect return operation.
block.append_operation(func::r#return( &[sum.result(0).unwrap().into()], location)); block.append_operation(
func::r#return( &[sum.result(0).unwrap().into()], location)
);
let region = Region::new(); let region = Region::new();
region.append_block(block); region.append_block(block);