Skip to content

Conversation

@ShangkunLi
Copy link
Collaborator

In this pr:

  • Add lowering from memref and builtin dialects
  • Add neura.load_indexed/store_indexed operation for memref like memory access
  • Fix some typos in files

Now, we can write code in cpp and lower it to affine dialect using polygeist. And more high-level transforms can be implemented in affine level, like polyhedral-based optimization, loop-unroll/fusion/fission/interchange/tiling/vectorize, etc.

@ShangkunLi ShangkunLi marked this pull request as ready for review June 20, 2025 08:55
@ShangkunLi ShangkunLi requested a review from tancheng June 20, 2025 08:55
@ShangkunLi
Copy link
Collaborator Author

Sry, some errors occurred when I try to transform bert_nodex.mlir into dataflow mlir.

Will fix it soon.

@ShangkunLi
Copy link
Collaborator Author

ShangkunLi commented Jun 20, 2025

Sry, some errors occurred when I try to transform bert_nodex.mlir into dataflow mlir.

Will fix it soon.

The problem arises from the TransformCtrlToDataFlowPass.cpp incorrectly handling nested loops. I may try to solve it.

  • Current pass cannot handle the cond_br that do not pass arguments, like
^bb3(%8: !neura.data<i64, i1>):  // 2 preds: ^bb2, ^bb4
    %9 = "neura.cast"(%8) <{cast_type = "int_to_index"}> : (!neura.data<i64, i1>) -> !neura.data<index, i1>
    %10 = "neura.icmp"(%9, %1) <{cmpType = "slt"}> : (!neura.data<index, i1>, !neura.data<index, i1>) -> !neura.data<i1, i1>
    neura.cond_br %10 : !neura.data<i1, i1> then to ^bb4 else to ^bb5
^bb4:  // pred: ^bb3
    %11 = neura.load_indexed %arg0[%2, %2, %2, %2, %2, %9 : !neura.data<index, i1>, !neura.data<index, i1>, !neura.data<index, i1>, !neura.data<index, i1>, !neura.data<index, i1>, !neura.data<index, i1>] memref<?x1x1x1x1x128xi8> : !neura.data<i8, i1>
    neura.store_indexed %11 to %arg1[%2, %2, %5, %2, %2, %9 : !neura.data<index, i1>, !neura.data<index, i1>, !neura.data<index, i1>, !neura.data<index, i1>, !neura.data<index, i1>, !neura.data<index, i1>] memref<?x1x128x1x1x128xi8> : !neura.data<i8, i1>
    %12 = "neura.add"(%9, %0) : (!neura.data<index, i1>, !neura.data<index, i1>) -> !neura.data<index, i1>
    %13 = "neura.cast"(%12) <{cast_type = "index_to_int"}> : (!neura.data<index, i1>) -> !neura.data<i64, i1>
    neura.br %13 : !neura.data<i64, i1> to ^bb3
  • Errors when inserting reserve & ctrl_mov for nested loop.

@tancheng
Copy link
Contributor

Sry, some errors occurred when I try to transform bert_nodex.mlir into dataflow mlir.
Will fix it soon.

The problem arises from the TransformCtrlToDataFlowPass.cpp incorrectly handling nested loops. I may try to solve it.

  • Current pass cannot handle the cond_br that do not pass arguments, like
^bb3(%8: !neura.data<i64, i1>):  // 2 preds: ^bb2, ^bb4
    %9 = "neura.cast"(%8) <{cast_type = "int_to_index"}> : (!neura.data<i64, i1>) -> !neura.data<index, i1>
    %10 = "neura.icmp"(%9, %1) <{cmpType = "slt"}> : (!neura.data<index, i1>, !neura.data<index, i1>) -> !neura.data<i1, i1>
    neura.cond_br %10 : !neura.data<i1, i1> then to ^bb4 else to ^bb5
^bb4:  // pred: ^bb3
    %11 = neura.load_indexed %arg0[%2, %2, %2, %2, %2, %9 : !neura.data<index, i1>, !neura.data<index, i1>, !neura.data<index, i1>, !neura.data<index, i1>, !neura.data<index, i1>, !neura.data<index, i1>] memref<?x1x1x1x1x128xi8> : !neura.data<i8, i1>
    neura.store_indexed %11 to %arg1[%2, %2, %5, %2, %2, %9 : !neura.data<index, i1>, !neura.data<index, i1>, !neura.data<index, i1>, !neura.data<index, i1>, !neura.data<index, i1>, !neura.data<index, i1>] memref<?x1x128x1x1x128xi8> : !neura.data<i8, i1>
    %12 = "neura.add"(%9, %0) : (!neura.data<index, i1>, !neura.data<index, i1>) -> !neura.data<index, i1>
    %13 = "neura.cast"(%12) <{cast_type = "index_to_int"}> : (!neura.data<index, i1>) -> !neura.data<i64, i1>
    neura.br %13 : !neura.data<i64, i1> to ^bb3
  • Errors when inserting reserve & ctrl_mov for nested loop.

Thanks @ShangkunLi, I didn't handle such case as GPT/Gemini told me MLIR has the rule that all basic block would only have arguments as live-ins (rather than directly use previously existing variable in other blocks), but it seems not always correct. Do you wanna fix this in this PR or later? I didn't see the --transform-ctrl-to-data is used in bert_node0.mlir?

@ShangkunLi
Copy link
Collaborator Author

Sry, some errors occurred when I try to transform bert_nodex.mlir into dataflow mlir.
Will fix it soon.

The problem arises from the TransformCtrlToDataFlowPass.cpp incorrectly handling nested loops. I may try to solve it.

  • Current pass cannot handle the cond_br that do not pass arguments, like
^bb3(%8: !neura.data<i64, i1>):  // 2 preds: ^bb2, ^bb4
    %9 = "neura.cast"(%8) <{cast_type = "int_to_index"}> : (!neura.data<i64, i1>) -> !neura.data<index, i1>
    %10 = "neura.icmp"(%9, %1) <{cmpType = "slt"}> : (!neura.data<index, i1>, !neura.data<index, i1>) -> !neura.data<i1, i1>
    neura.cond_br %10 : !neura.data<i1, i1> then to ^bb4 else to ^bb5
^bb4:  // pred: ^bb3
    %11 = neura.load_indexed %arg0[%2, %2, %2, %2, %2, %9 : !neura.data<index, i1>, !neura.data<index, i1>, !neura.data<index, i1>, !neura.data<index, i1>, !neura.data<index, i1>, !neura.data<index, i1>] memref<?x1x1x1x1x128xi8> : !neura.data<i8, i1>
    neura.store_indexed %11 to %arg1[%2, %2, %5, %2, %2, %9 : !neura.data<index, i1>, !neura.data<index, i1>, !neura.data<index, i1>, !neura.data<index, i1>, !neura.data<index, i1>, !neura.data<index, i1>] memref<?x1x128x1x1x128xi8> : !neura.data<i8, i1>
    %12 = "neura.add"(%9, %0) : (!neura.data<index, i1>, !neura.data<index, i1>) -> !neura.data<index, i1>
    %13 = "neura.cast"(%12) <{cast_type = "index_to_int"}> : (!neura.data<index, i1>) -> !neura.data<i64, i1>
    neura.br %13 : !neura.data<i64, i1> to ^bb3
  • Errors when inserting reserve & ctrl_mov for nested loop.

Thanks @ShangkunLi, I didn't handle such case as GPT/Gemini told me MLIR has the rule that all basic block would only have arguments as live-ins (rather than directly use previously existing variable in other blocks), but it seems not always correct. Do you wanna fix this in this PR or later? I didn't see the --transform-ctrl-to-data is used in bert_node0.mlir?

Filed an issue #54. I may try to fix this in the next pr. For this pr, I just tested these lowering patterns.

Copy link
Contributor

@tancheng tancheng left a comment

Choose a reason for hiding this comment

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

Thanks for the PR, let's wait for the pass on github action before merging :-)

@ShangkunLi ShangkunLi merged commit 805548c into coredac:main Jun 20, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

new feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants