Skip to content
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

[Upstream] Decouple the Triton GPU lowering utils. #992

Closed

Conversation

chengjunlu
Copy link
Contributor

@chengjunlu chengjunlu commented Apr 29, 2024

The Triton GPU lowering util support dispatching those utils to the layout interface.
Decouple the basic util function emitIndices, emitOffsetForLayout and emitBaseIndexForLayout.

The layout specific difference is abstracted by the layout interface.
The target specific difference is abstracted by TargetInfo class.

Then we can reuse the Triton GPU lowering.

@etiotto
Copy link
Contributor

etiotto commented Apr 29, 2024

Very large PR. We should split it.

Copy link
Contributor

@whitneywhtsang whitneywhtsang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is WIP. Just for review comments.

What do you still plan to improve? Use common conversion files as upstream?

include/triton/Conversion/TritonGPUToLLVM/Utility.h Outdated Show resolved Hide resolved
@chengjunlu
Copy link
Contributor Author

This is WIP. Just for review comments.

What do you still plan to improve? Use common conversion files as upstream?

Yes. The steps of decoupling code are:

  1. Abstract the layout related interface and add the AttrInterface to the public TritonGPUAttrDef.td.
  2. Abstract the target related interface and add to the TargetInfo classes.
  3. Reuse the common lowering conversion files as possible.
  4. In the third_party Intel, we make it a passes collection.

@etiotto etiotto requested a review from victor-eds April 30, 2024 14:08
Copy link
Contributor

@victor-eds victor-eds left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the file was moved, I am not sure what changes are actually new. I just reviewed the whole of it.

third_party/intel/include/TritonIntelGPUToLLVM/Utility.h Outdated Show resolved Hide resolved
third_party/intel/include/TritonIntelGPUToLLVM/Utility.h Outdated Show resolved Hide resolved
Comment on lines 27 to 31
/// Create a predicated block, using \p cond as the condition and \p ops for the
/// values supplied by the conditional branch to the exit block. The \p
/// thenOpsFn function is used to inject operations in the 'then' branch:
/// cf.cond_br %cond, ^br1, ^br2(%ops)
/// ^br1:
/// %then_ops = `thenOpsFn()`
/// cf.br ^br2(%then_ops)
/// ^br2(%block_ops):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use markdown for formatting

Comment on lines 50 to 52
assert(llvm::all_of(llvm::enumerate(ops, thenOps),
[](const auto &enumerator) {
auto [index, op, thenOp] = enumerator;
return op.getType() == thenOp.getType();
}) &&
"type mismatch found");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert(llvm::all_of(llvm::enumerate(ops, thenOps),
[](const auto &enumerator) {
auto [index, op, thenOp] = enumerator;
return op.getType() == thenOp.getType();
}) &&
"type mismatch found");
assert(llvm::equal(ops, thenOps, ...) && "type mismatch found");

template <typename ThenOpsFn>
Block &createPredicatedBlock(ConversionPatternRewriter &rewriter, Location loc,
Value cond, ArrayRef<Value> ops,
ThenOpsFn &&thenOpsFn) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
template <typename ThenOpsFn>
Block &createPredicatedBlock(ConversionPatternRewriter &rewriter, Location loc,
Value cond, ArrayRef<Value> ops,
ThenOpsFn &&thenOpsFn) {
template <typename F>
Block &createPredicatedBlock(RewriterBase &rewriter, Location loc,
Value cond, ValueRange ops,
F thenOpsFn) {

Can we also have more specific names for ops and thenOpsFn? Maybe branchBlockArgs and endBlockArgsGen?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be more readable to generate scf operations here? If not, why not go down and use llvm.br right away?

third_party/intel/include/TritonIntelGPUToLLVM/Utility.h Outdated Show resolved Hide resolved
third_party/intel/include/TritonIntelGPUToLLVM/Utility.h Outdated Show resolved Hide resolved
third_party/intel/include/TritonIntelGPUToLLVM/Utility.h Outdated Show resolved Hide resolved
third_party/intel/include/TritonIntelGPUToLLVM/Utility.h Outdated Show resolved Hide resolved

static SmallVector<Value>
emitBaseIndexForDpasLayout(Location loc, RewriterBase &rewriter,
const DpasEncodingAttr &dpasLayout,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const DpasEncodingAttr &dpasLayout,
DpasEncodingAttr dpasLayout,

Same for other Attribute args

@chengjunlu chengjunlu force-pushed the chengjun/llvm-target-decouple-emitIndex branch 2 times, most recently from 75e76b7 to d01bdca Compare May 8, 2024 05:50
@chengjunlu
Copy link
Contributor Author

As the file was moved, I am not sure what changes are actually new. I just reviewed the whole of it.

You can use this commit to review the changes in the Utility.h
d01bdca

@victor-eds
Copy link
Contributor

You can use this commit to review the changes in the Utility.h

Utility.h changes LGTM

… and `emitBaseIndexForLayout`. The Triton GPU lowering util support dispatching those utils to the layout interface.
Comment on lines +530 to +540
InterfaceMethod<"emit base index",
"SmallVector<Value>",
"emitBaseIndexWithinCTAForLayout",
(ins "Location":$loc,
"RewriterBase&":$rewriter,
"RankedTensorType":$type)>,

InterfaceMethod<"emit offset",
"SmallVector<SmallVector<unsigned>>",
"emitOffsetForLayout",
(ins "RankedTensorType":$type)>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sidenote: you can provide a default implementation changing this to:

Suggested change
InterfaceMethod<"emit base index",
"SmallVector<Value>",
"emitBaseIndexWithinCTAForLayout",
(ins "Location":$loc,
"RewriterBase&":$rewriter,
"RankedTensorType":$type)>,
InterfaceMethod<"emit offset",
"SmallVector<SmallVector<unsigned>>",
"emitOffsetForLayout",
(ins "RankedTensorType":$type)>,
InterfaceMethod<"emit base index",
"SmallVector<Value>",
"emitBaseIndexWithinCTAForLayout",
(ins "Location":$loc,
"RewriterBase&":$rewriter,
"RankedTensorType":$type),
/*methodBody=*/[{}],
/*defaultImplementation=*/[{
// IMPL
}]>,
InterfaceMethod<"emit offset",
"SmallVector<SmallVector<unsigned>>",
"emitOffsetForLayout",
(ins "RankedTensorType":$type),
/*methodBody=*/"",
/*defaultImplementation=*/[{
// IMPL
}]>,

Then, in attriburtes implementing this interface, instead of inheriting like:

[InterfaceFoo]

do:

[DeclareAttrInterfaceMethods<InterfaceFoo>]

See for context

Copy link
Contributor

@whitneywhtsang whitneywhtsang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please explain the motivation to move Utility.h from lib to include? It is in lib for the other backends.

@whitneywhtsang
Copy link
Contributor

Can you please explain the motivation to move Utility.h from lib to include? It is in lib for the other backends.

I have moved it back in 2faf5ed, please take a look to see if you are ok with it.

whitneywhtsang added a commit that referenced this pull request May 8, 2024
@whitneywhtsang
Copy link
Contributor

Other than adding default implementation as suggested in #992 (comment), this PR is ready to upstream IMO.

@chengjunlu
Copy link
Contributor Author

Can you please explain the motivation to move Utility.h from lib to include? It is in lib for the other backends.

I have moved it back in 2faf5ed, please take a look to see if you are ok with it.

Looks perfect to me.

@victor-eds
Copy link
Contributor

Current state LGTM. If the default definition is to be added in a different PR, we can keep as is. 👍

@whitneywhtsang whitneywhtsang marked this pull request as draft May 13, 2024 16:11
@chengjunlu chengjunlu closed this May 14, 2024
@chengjunlu chengjunlu deleted the chengjun/llvm-target-decouple-emitIndex branch May 31, 2024 04:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Upstream] Decouple the duplicated code in Triton Intel Utility to avoid copy the Triton lowering code.
4 participants