-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[MLIR][XeGPU] Support subview memref: handling the base address during xegpu to xevm type conversion #170541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
[MLIR][XeGPU] Support subview memref: handling the base address during xegpu to xevm type conversion #170541
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -991,27 +991,70 @@ struct ConvertXeGPUToXeVMPass | |
| }); | ||
|
|
||
| typeConverter.addConversion([&](MemRefType type) -> Type { | ||
| if (type.getMemorySpaceAsInt() == 3) | ||
| return IntegerType::get(&getContext(), 32); | ||
| return IntegerType::get(&getContext(), 64); | ||
| return IntegerType::get(&getContext(), | ||
| (xegpu::isSharedMemRef(type) ? 32 : 64)); | ||
| }); | ||
|
|
||
| // LLVM type converter puts unrealized casts for the following cases: | ||
| // add materialization casts to handle them. | ||
|
|
||
| // Materialization to convert memref to i64 | ||
| // Materialization to convert memref to i64 or i32 depending on global/SLM | ||
| auto memrefMaterializationCast = [](OpBuilder &builder, Type type, | ||
| ValueRange inputs, | ||
| Location loc) -> Value { | ||
| if (inputs.size() != 1) | ||
| return {}; | ||
| auto input = inputs.front(); | ||
| if (auto memrefTy = dyn_cast<MemRefType>(input.getType())) { | ||
| unsigned rank = memrefTy.getRank(); | ||
| Type indexType = builder.getIndexType(); | ||
|
|
||
| Value addr = | ||
| memref::ExtractAlignedPointerAsIndexOp::create(builder, loc, input); | ||
| return arith::IndexCastUIOp::create(builder, loc, type, addr) | ||
| .getResult(); | ||
| int64_t intOffsets; | ||
| SmallVector<int64_t> intStrides; | ||
| Value addr; | ||
| Value offset; | ||
| if (failed(memrefTy.getStridesAndOffset(intStrides, intOffsets))) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why this check? why not use |
||
|
|
||
| // Result types: [base_memref, offset, stride0, stride1, ..., | ||
| // strideN-1, size0, size1, ..., sizeN-1] | ||
| SmallVector<Type> resultTypes{ | ||
| MemRefType::get({}, memrefTy.getElementType(), | ||
| MemRefLayoutAttrInterface(), | ||
| memrefTy.getMemorySpace()), | ||
| indexType}; | ||
| // strides + sizes | ||
| resultTypes.append(2 * rank, indexType); | ||
|
|
||
| auto meta = memref::ExtractStridedMetadataOp::create( | ||
| builder, loc, resultTypes, input); | ||
|
|
||
| addr = memref::ExtractAlignedPointerAsIndexOp::create( | ||
| builder, loc, meta.getBaseBuffer()); | ||
| offset = meta.getOffset(); | ||
|
|
||
| } else { | ||
| addr = memref::ExtractAlignedPointerAsIndexOp::create(builder, loc, | ||
| input); | ||
| offset = arith::ConstantOp::create(builder, loc, | ||
| builder.getIndexAttr(intOffsets)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this could be |
||
| } | ||
|
|
||
| auto addr_casted = | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LLVM does not use snake case variable naming. rename to |
||
| arith::IndexCastUIOp::create(builder, loc, type, addr); | ||
| auto offset_casted = | ||
| arith::IndexCastUIOp::create(builder, loc, type, offset); | ||
|
|
||
| // Compute the final address: base address + byte offset | ||
| auto byte_size = arith::ConstantOp::create( | ||
| builder, loc, type, | ||
| builder.getIntegerAttr(type, | ||
| memrefTy.getElementTypeBitWidth() / 8)); | ||
| auto byte_offset = | ||
| arith::MulIOp::create(builder, loc, offset_casted, byte_size); | ||
| auto addr_with_offset = | ||
| arith::AddIOp::create(builder, loc, addr_casted, byte_offset); | ||
|
|
||
| return addr_with_offset.getResult(); | ||
| } | ||
| return {}; | ||
| }; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: This is only used in a single file for now (
XeGPUToXeVM). For now you can have this as a helper inside that file. If more uses arise we can have a common place for it.