[PATCH] D121076: [MLIR][Python] Add SCFIfOp Python binding
chhzh123 added a comment. Passed pre-merge checks. Should be able to be merged. Thanks! Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D121076/new/ https://reviews.llvm.org/D121076 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D121076: [MLIR][Python] Add SCFIfOp Python binding
chhzh123 updated this revision to Diff 414900. chhzh123 added a comment. Rebase Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D121076/new/ https://reviews.llvm.org/D121076 Files: mlir/python/mlir/dialects/_scf_ops_ext.py mlir/test/python/dialects/scf.py Index: mlir/test/python/dialects/scf.py === --- mlir/test/python/dialects/scf.py +++ mlir/test/python/dialects/scf.py @@ -82,3 +82,58 @@ # CHECK: iter_args(%{{.*}} = %[[ARGS]]#0, %{{.*}} = %[[ARGS]]#1) # CHECK: scf.yield %{{.*}}, %{{.*}} # CHECK: return + + +@constructAndPrintInModule +def testIfWithoutElse(): + bool = IntegerType.get_signless(1) + i32 = IntegerType.get_signless(32) + + @builtin.FuncOp.from_py_func(bool) + def simple_if(cond): +if_op = scf.IfOp(cond) +with InsertionPoint(if_op.then_block): + one = arith.ConstantOp(i32, 1) + add = arith.AddIOp(one, one) + scf.YieldOp([]) +return + + +# CHECK: func @simple_if(%[[ARG0:.*]]: i1) +# CHECK: scf.if %[[ARG0:.*]] +# CHECK: %[[ONE:.*]] = arith.constant 1 +# CHECK: %[[ADD:.*]] = arith.addi %[[ONE]], %[[ONE]] +# CHECK: return + + +@constructAndPrintInModule +def testIfWithElse(): + bool = IntegerType.get_signless(1) + i32 = IntegerType.get_signless(32) + + @builtin.FuncOp.from_py_func(bool) + def simple_if_else(cond): +if_op = scf.IfOp(cond, [i32, i32], hasElse=True) +with InsertionPoint(if_op.then_block): + x_true = arith.ConstantOp(i32, 0) + y_true = arith.ConstantOp(i32, 1) + scf.YieldOp([x_true, y_true]) +with InsertionPoint(if_op.else_block): + x_false = arith.ConstantOp(i32, 2) + y_false = arith.ConstantOp(i32, 3) + scf.YieldOp([x_false, y_false]) +add = arith.AddIOp(if_op.results[0], if_op.results[1]) +return + + +# CHECK: func @simple_if_else(%[[ARG0:.*]]: i1) +# CHECK: %[[RET:.*]]:2 = scf.if %[[ARG0:.*]] +# CHECK: %[[ZERO:.*]] = arith.constant 0 +# CHECK: %[[ONE:.*]] = arith.constant 1 +# CHECK: scf.yield %[[ZERO]], %[[ONE]] +# CHECK: } else { +# CHECK: %[[TWO:.*]] = arith.constant 2 +# CHECK: %[[THREE:.*]] = arith.constant 3 +# CHECK: scf.yield %[[TWO]], %[[THREE]] +# CHECK: arith.addi %[[RET]]#0, %[[RET]]#1 +# CHECK: return Index: mlir/python/mlir/dialects/_scf_ops_ext.py === --- mlir/python/mlir/dialects/_scf_ops_ext.py +++ mlir/python/mlir/dialects/_scf_ops_ext.py @@ -64,3 +64,44 @@ To obtain the loop-carried operands, use `iter_args`. """ return self.body.arguments[1:] + + +class IfOp: + """Specialization for the SCF if op class.""" + + def __init__(self, + cond, + results_=[], + *, + hasElse=False, + loc=None, + ip=None): +"""Creates an SCF `if` operation. + +- `cond` is a MLIR value of 'i1' type to determine which regions of code will be executed. +- `hasElse` determines whether the if operation has the else branch. +""" +operands = [] +operands.append(cond) +results = [] +results.extend(results_) +super().__init__( +self.build_generic( +regions=2, +results=results, +operands=operands, +loc=loc, +ip=ip)) +self.regions[0].blocks.append(*[]) +if hasElse: +self.regions[1].blocks.append(*[]) + + @property + def then_block(self): +"""Returns the then block of the if operation.""" +return self.regions[0].blocks[0] + + @property + def else_block(self): +"""Returns the else block of the if operation.""" +return self.regions[1].blocks[0] ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D121076: [MLIR][Python] Add SCFIfOp Python binding
chhzh123 added a comment. I've rebased to the latest commit in the main branch, but the pre-merge checks still failed. Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D121076/new/ https://reviews.llvm.org/D121076 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D121076: [MLIR][Python] Add SCFIfOp Python binding
chhzh123 updated this revision to Diff 414852. chhzh123 added a comment. Try rebasing again Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D121076/new/ https://reviews.llvm.org/D121076 Files: mlir/python/mlir/dialects/_scf_ops_ext.py mlir/test/python/dialects/scf.py Index: mlir/test/python/dialects/scf.py === --- mlir/test/python/dialects/scf.py +++ mlir/test/python/dialects/scf.py @@ -82,3 +82,58 @@ # CHECK: iter_args(%{{.*}} = %[[ARGS]]#0, %{{.*}} = %[[ARGS]]#1) # CHECK: scf.yield %{{.*}}, %{{.*}} # CHECK: return + + +@constructAndPrintInModule +def testIfWithoutElse(): + bool = IntegerType.get_signless(1) + i32 = IntegerType.get_signless(32) + + @builtin.FuncOp.from_py_func(bool) + def simple_if(cond): +if_op = scf.IfOp(cond) +with InsertionPoint(if_op.then_block): + one = arith.ConstantOp(i32, 1) + add = arith.AddIOp(one, one) + scf.YieldOp([]) +return + + +# CHECK: func @simple_if(%[[ARG0:.*]]: i1) +# CHECK: scf.if %[[ARG0:.*]] +# CHECK: %[[ONE:.*]] = arith.constant 1 +# CHECK: %[[ADD:.*]] = arith.addi %[[ONE]], %[[ONE]] +# CHECK: return + + +@constructAndPrintInModule +def testIfWithElse(): + bool = IntegerType.get_signless(1) + i32 = IntegerType.get_signless(32) + + @builtin.FuncOp.from_py_func(bool) + def simple_if_else(cond): +if_op = scf.IfOp(cond, [i32, i32], hasElse=True) +with InsertionPoint(if_op.then_block): + x_true = arith.ConstantOp(i32, 0) + y_true = arith.ConstantOp(i32, 1) + scf.YieldOp([x_true, y_true]) +with InsertionPoint(if_op.else_block): + x_false = arith.ConstantOp(i32, 2) + y_false = arith.ConstantOp(i32, 3) + scf.YieldOp([x_false, y_false]) +add = arith.AddIOp(if_op.results[0], if_op.results[1]) +return + + +# CHECK: func @simple_if_else(%[[ARG0:.*]]: i1) +# CHECK: %[[RET:.*]]:2 = scf.if %[[ARG0:.*]] +# CHECK: %[[ZERO:.*]] = arith.constant 0 +# CHECK: %[[ONE:.*]] = arith.constant 1 +# CHECK: scf.yield %[[ZERO]], %[[ONE]] +# CHECK: } else { +# CHECK: %[[TWO:.*]] = arith.constant 2 +# CHECK: %[[THREE:.*]] = arith.constant 3 +# CHECK: scf.yield %[[TWO]], %[[THREE]] +# CHECK: arith.addi %[[RET]]#0, %[[RET]]#1 +# CHECK: return Index: mlir/python/mlir/dialects/_scf_ops_ext.py === --- mlir/python/mlir/dialects/_scf_ops_ext.py +++ mlir/python/mlir/dialects/_scf_ops_ext.py @@ -70,16 +70,16 @@ """Specialization for the SCF if op class.""" def __init__(self, - results_, cond, - withElseRegion=False, + results_=[], *, + hasElse=False, loc=None, ip=None): """Creates an SCF `if` operation. -- `cond` is a boolean value to determine which regions of code will be executed. -- `withElseRegion` determines whether the if operation has the else branch. +- `cond` is a MLIR value of 'i1' type to determine which regions of code will be executed. +- `hasElse` determines whether the if operation has the else branch. """ operands = [] operands.append(cond) @@ -92,9 +92,9 @@ operands=operands, loc=loc, ip=ip)) -self.regions[0].blocks.append(*results) -if withElseRegion: -self.regions[1].blocks.append(*results) +self.regions[0].blocks.append(*[]) +if hasElse: +self.regions[1].blocks.append(*[]) @property def then_block(self): ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D121076: [MLIR][Python] Add SCFIfOp Python binding
chhzh123 updated this revision to Diff 414847. chhzh123 added a comment. Rebase 3 commits Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D121076/new/ https://reviews.llvm.org/D121076 Files: mlir/python/mlir/dialects/_scf_ops_ext.py mlir/test/python/dialects/scf.py Index: mlir/test/python/dialects/scf.py === --- mlir/test/python/dialects/scf.py +++ mlir/test/python/dialects/scf.py @@ -82,3 +82,58 @@ # CHECK: iter_args(%{{.*}} = %[[ARGS]]#0, %{{.*}} = %[[ARGS]]#1) # CHECK: scf.yield %{{.*}}, %{{.*}} # CHECK: return + + +@constructAndPrintInModule +def testIfWithoutElse(): + bool = IntegerType.get_signless(1) + i32 = IntegerType.get_signless(32) + + @builtin.FuncOp.from_py_func(bool) + def simple_if(cond): +if_op = scf.IfOp(cond) +with InsertionPoint(if_op.then_block): + one = arith.ConstantOp(i32, 1) + add = arith.AddIOp(one, one) + scf.YieldOp([]) +return + + +# CHECK: func @simple_if(%[[ARG0:.*]]: i1) +# CHECK: scf.if %[[ARG0:.*]] +# CHECK: %[[ONE:.*]] = arith.constant 1 +# CHECK: %[[ADD:.*]] = arith.addi %[[ONE]], %[[ONE]] +# CHECK: return + + +@constructAndPrintInModule +def testIfWithElse(): + bool = IntegerType.get_signless(1) + i32 = IntegerType.get_signless(32) + + @builtin.FuncOp.from_py_func(bool) + def simple_if_else(cond): +if_op = scf.IfOp(cond, [i32, i32], hasElse=True) +with InsertionPoint(if_op.then_block): + x_true = arith.ConstantOp(i32, 0) + y_true = arith.ConstantOp(i32, 1) + scf.YieldOp([x_true, y_true]) +with InsertionPoint(if_op.else_block): + x_false = arith.ConstantOp(i32, 2) + y_false = arith.ConstantOp(i32, 3) + scf.YieldOp([x_false, y_false]) +add = arith.AddIOp(if_op.results[0], if_op.results[1]) +return + + +# CHECK: func @simple_if_else(%[[ARG0:.*]]: i1) +# CHECK: %[[RET:.*]]:2 = scf.if %[[ARG0:.*]] +# CHECK: %[[ZERO:.*]] = arith.constant 0 +# CHECK: %[[ONE:.*]] = arith.constant 1 +# CHECK: scf.yield %[[ZERO]], %[[ONE]] +# CHECK: } else { +# CHECK: %[[TWO:.*]] = arith.constant 2 +# CHECK: %[[THREE:.*]] = arith.constant 3 +# CHECK: scf.yield %[[TWO]], %[[THREE]] +# CHECK: arith.addi %[[RET]]#0, %[[RET]]#1 +# CHECK: return Index: mlir/python/mlir/dialects/_scf_ops_ext.py === --- mlir/python/mlir/dialects/_scf_ops_ext.py +++ mlir/python/mlir/dialects/_scf_ops_ext.py @@ -70,16 +70,16 @@ """Specialization for the SCF if op class.""" def __init__(self, - results_, cond, - withElseRegion=False, + results_=[], *, + hasElse=False, loc=None, ip=None): """Creates an SCF `if` operation. -- `cond` is a boolean value to determine which regions of code will be executed. -- `withElseRegion` determines whether the if operation has the else branch. +- `cond` is a MLIR value of 'i1' type to determine which regions of code will be executed. +- `hasElse` determines whether the if operation has the else branch. """ operands = [] operands.append(cond) @@ -92,9 +92,9 @@ operands=operands, loc=loc, ip=ip)) -self.regions[0].blocks.append(*results) -if withElseRegion: -self.regions[1].blocks.append(*results) +self.regions[0].blocks.append(*[]) +if hasElse: +self.regions[1].blocks.append(*[]) @property def then_block(self): ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D121076: [MLIR][Python] Add SCFIfOp Python binding
chhzh123 updated this revision to Diff 414845. chhzh123 added a comment. Rebase Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D121076/new/ https://reviews.llvm.org/D121076 Files: mlir/test/python/dialects/scf.py Index: mlir/test/python/dialects/scf.py === --- mlir/test/python/dialects/scf.py +++ mlir/test/python/dialects/scf.py @@ -82,3 +82,58 @@ # CHECK: iter_args(%{{.*}} = %[[ARGS]]#0, %{{.*}} = %[[ARGS]]#1) # CHECK: scf.yield %{{.*}}, %{{.*}} # CHECK: return + + +@constructAndPrintInModule +def testIfWithoutElse(): + bool = IntegerType.get_signless(1) + i32 = IntegerType.get_signless(32) + + @builtin.FuncOp.from_py_func(bool) + def simple_if(cond): +if_op = scf.IfOp(cond) +with InsertionPoint(if_op.then_block): + one = arith.ConstantOp(i32, 1) + add = arith.AddIOp(one, one) + scf.YieldOp([]) +return + + +# CHECK: func @simple_if(%[[ARG0:.*]]: i1) +# CHECK: scf.if %[[ARG0:.*]] +# CHECK: %[[ONE:.*]] = arith.constant 1 +# CHECK: %[[ADD:.*]] = arith.addi %[[ONE]], %[[ONE]] +# CHECK: return + + +@constructAndPrintInModule +def testIfWithElse(): + bool = IntegerType.get_signless(1) + i32 = IntegerType.get_signless(32) + + @builtin.FuncOp.from_py_func(bool) + def simple_if_else(cond): +if_op = scf.IfOp(cond, [i32, i32], hasElse=True) +with InsertionPoint(if_op.then_block): + x_true = arith.ConstantOp(i32, 0) + y_true = arith.ConstantOp(i32, 1) + scf.YieldOp([x_true, y_true]) +with InsertionPoint(if_op.else_block): + x_false = arith.ConstantOp(i32, 2) + y_false = arith.ConstantOp(i32, 3) + scf.YieldOp([x_false, y_false]) +add = arith.AddIOp(if_op.results[0], if_op.results[1]) +return + + +# CHECK: func @simple_if_else(%[[ARG0:.*]]: i1) +# CHECK: %[[RET:.*]]:2 = scf.if %[[ARG0:.*]] +# CHECK: %[[ZERO:.*]] = arith.constant 0 +# CHECK: %[[ONE:.*]] = arith.constant 1 +# CHECK: scf.yield %[[ZERO]], %[[ONE]] +# CHECK: } else { +# CHECK: %[[TWO:.*]] = arith.constant 2 +# CHECK: %[[THREE:.*]] = arith.constant 3 +# CHECK: scf.yield %[[TWO]], %[[THREE]] +# CHECK: arith.addi %[[RET]]#0, %[[RET]]#1 +# CHECK: return Index: mlir/test/python/dialects/scf.py === --- mlir/test/python/dialects/scf.py +++ mlir/test/python/dialects/scf.py @@ -82,3 +82,58 @@ # CHECK: iter_args(%{{.*}} = %[[ARGS]]#0, %{{.*}} = %[[ARGS]]#1) # CHECK: scf.yield %{{.*}}, %{{.*}} # CHECK: return + + +@constructAndPrintInModule +def testIfWithoutElse(): + bool = IntegerType.get_signless(1) + i32 = IntegerType.get_signless(32) + + @builtin.FuncOp.from_py_func(bool) + def simple_if(cond): +if_op = scf.IfOp(cond) +with InsertionPoint(if_op.then_block): + one = arith.ConstantOp(i32, 1) + add = arith.AddIOp(one, one) + scf.YieldOp([]) +return + + +# CHECK: func @simple_if(%[[ARG0:.*]]: i1) +# CHECK: scf.if %[[ARG0:.*]] +# CHECK: %[[ONE:.*]] = arith.constant 1 +# CHECK: %[[ADD:.*]] = arith.addi %[[ONE]], %[[ONE]] +# CHECK: return + + +@constructAndPrintInModule +def testIfWithElse(): + bool = IntegerType.get_signless(1) + i32 = IntegerType.get_signless(32) + + @builtin.FuncOp.from_py_func(bool) + def simple_if_else(cond): +if_op = scf.IfOp(cond, [i32, i32], hasElse=True) +with InsertionPoint(if_op.then_block): + x_true = arith.ConstantOp(i32, 0) + y_true = arith.ConstantOp(i32, 1) + scf.YieldOp([x_true, y_true]) +with InsertionPoint(if_op.else_block): + x_false = arith.ConstantOp(i32, 2) + y_false = arith.ConstantOp(i32, 3) + scf.YieldOp([x_false, y_false]) +add = arith.AddIOp(if_op.results[0], if_op.results[1]) +return + + +# CHECK: func @simple_if_else(%[[ARG0:.*]]: i1) +# CHECK: %[[RET:.*]]:2 = scf.if %[[ARG0:.*]] +# CHECK: %[[ZERO:.*]] = arith.constant 0 +# CHECK: %[[ONE:.*]] = arith.constant 1 +# CHECK: scf.yield %[[ZERO]], %[[ONE]] +# CHECK: } else { +# CHECK: %[[TWO:.*]] = arith.constant 2 +# CHECK: %[[THREE:.*]] = arith.constant 3 +# CHECK: scf.yield %[[TWO]], %[[THREE]] +# CHECK: arith.addi %[[RET]]#0, %[[RET]]#1 +# CHECK: return ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D121076: [MLIR][Python] Add SCFIfOp Python binding
chhzh123 added a comment. @ftynse Thanks for reviewing! Could you help land the PR? Seems I do not have access rights to push to the main repository. Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D121076/new/ https://reviews.llvm.org/D121076 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D121076: [MLIR][Python] Add SCFIfOp Python binding
chhzh123 added a comment. Hi @ftynse , could you please take a look at it and see if there is anything that should be changed? Thanks! Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D121076/new/ https://reviews.llvm.org/D121076 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D121076: [MLIR][Python] Add SCFIfOp Python binding
chhzh123 marked 2 inline comments as done. chhzh123 added a comment. @ftynse Sorry for messing up this PR a bit. I've made the changes you suggested. Please have a check. Thanks! Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D121076/new/ https://reviews.llvm.org/D121076 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D121076: [MLIR][Python] Add SCFIfOp Python binding
chhzh123 updated this revision to Diff 413532. chhzh123 added a comment. Fix Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D121076/new/ https://reviews.llvm.org/D121076 Files: mlir/python/mlir/dialects/_scf_ops_ext.py mlir/test/python/dialects/scf.py Index: mlir/test/python/dialects/scf.py === --- mlir/test/python/dialects/scf.py +++ mlir/test/python/dialects/scf.py @@ -82,3 +82,58 @@ # CHECK: iter_args(%{{.*}} = %[[ARGS]]#0, %{{.*}} = %[[ARGS]]#1) # CHECK: scf.yield %{{.*}}, %{{.*}} # CHECK: return + + +@constructAndPrintInModule +def testIfWithoutElse(): + bool = IntegerType.get_signless(1) + i32 = IntegerType.get_signless(32) + + @builtin.FuncOp.from_py_func(bool) + def simple_if(cond): +if_op = scf.IfOp(cond) +with InsertionPoint(if_op.then_block): + one = arith.ConstantOp(i32, 1) + add = arith.AddIOp(one, one) + scf.YieldOp([]) +return + + +# CHECK: func @simple_if(%[[ARG0:.*]]: i1) +# CHECK: scf.if %[[ARG0:.*]] +# CHECK: %[[ONE:.*]] = arith.constant 1 +# CHECK: %[[ADD:.*]] = arith.addi %[[ONE]], %[[ONE]] +# CHECK: return + + +@constructAndPrintInModule +def testIfWithElse(): + bool = IntegerType.get_signless(1) + i32 = IntegerType.get_signless(32) + + @builtin.FuncOp.from_py_func(bool) + def simple_if_else(cond): +if_op = scf.IfOp(cond, [i32, i32], hasElse=True) +with InsertionPoint(if_op.then_block): + x_true = arith.ConstantOp(i32, 0) + y_true = arith.ConstantOp(i32, 1) + scf.YieldOp([x_true, y_true]) +with InsertionPoint(if_op.else_block): + x_false = arith.ConstantOp(i32, 2) + y_false = arith.ConstantOp(i32, 3) + scf.YieldOp([x_false, y_false]) +add = arith.AddIOp(if_op.results[0], if_op.results[1]) +return + + +# CHECK: func @simple_if_else(%[[ARG0:.*]]: i1) +# CHECK: %[[RET:.*]]:2 = scf.if %[[ARG0:.*]] +# CHECK: %[[ZERO:.*]] = arith.constant 0 +# CHECK: %[[ONE:.*]] = arith.constant 1 +# CHECK: scf.yield %[[ZERO]], %[[ONE]] +# CHECK: } else { +# CHECK: %[[TWO:.*]] = arith.constant 2 +# CHECK: %[[THREE:.*]] = arith.constant 3 +# CHECK: scf.yield %[[TWO]], %[[THREE]] +# CHECK: arith.addi %[[RET]]#0, %[[RET]]#1 +# CHECK: return Index: mlir/python/mlir/dialects/_scf_ops_ext.py === --- mlir/python/mlir/dialects/_scf_ops_ext.py +++ mlir/python/mlir/dialects/_scf_ops_ext.py @@ -70,16 +70,16 @@ """Specialization for the SCF if op class.""" def __init__(self, - results_, cond, - withElseRegion=False, + results_=[], *, + hasElse=False, loc=None, ip=None): """Creates an SCF `if` operation. -- `cond` is a boolean value to determine which regions of code will be executed. -- `withElseRegion` determines whether the if operation has the else branch. +- `cond` is a MLIR value of 'i1' type to determine which regions of code will be executed. +- `hasElse` determines whether the if operation has the else branch. """ operands = [] operands.append(cond) @@ -92,9 +92,9 @@ operands=operands, loc=loc, ip=ip)) -self.regions[0].blocks.append(*results) -if withElseRegion: -self.regions[1].blocks.append(*results) +self.regions[0].blocks.append(*[]) +if hasElse: +self.regions[1].blocks.append(*[]) @property def then_block(self): ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D121076: [MLIR][Python] Add SCFIfOp Python binding
chhzh123 updated this revision to Diff 413528. chhzh123 added a comment. Fix function signature and add tests Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D121076/new/ https://reviews.llvm.org/D121076 Files: mlir/test/python/dialects/scf.py Index: mlir/test/python/dialects/scf.py === --- mlir/test/python/dialects/scf.py +++ mlir/test/python/dialects/scf.py @@ -82,3 +82,58 @@ # CHECK: iter_args(%{{.*}} = %[[ARGS]]#0, %{{.*}} = %[[ARGS]]#1) # CHECK: scf.yield %{{.*}}, %{{.*}} # CHECK: return + + +@constructAndPrintInModule +def testIfWithoutElse(): + bool = IntegerType.get_signless(1) + i32 = IntegerType.get_signless(32) + + @builtin.FuncOp.from_py_func(bool) + def simple_if(cond): +if_op = scf.IfOp(cond) +with InsertionPoint(if_op.then_block): + one = arith.ConstantOp(i32, 1) + add = arith.AddIOp(one, one) + scf.YieldOp([]) +return + + +# CHECK: func @simple_if(%[[ARG0:.*]]: i1) +# CHECK: scf.if %[[ARG0:.*]] +# CHECK: %[[ONE:.*]] = arith.constant 1 +# CHECK: %[[ADD:.*]] = arith.addi %[[ONE]], %[[ONE]] +# CHECK: return + + +@constructAndPrintInModule +def testIfWithElse(): + bool = IntegerType.get_signless(1) + i32 = IntegerType.get_signless(32) + + @builtin.FuncOp.from_py_func(bool) + def simple_if_else(cond): +if_op = scf.IfOp(cond, [i32, i32], hasElse=True) +with InsertionPoint(if_op.then_block): + x_true = arith.ConstantOp(i32, 0) + y_true = arith.ConstantOp(i32, 1) + scf.YieldOp([x_true, y_true]) +with InsertionPoint(if_op.else_block): + x_false = arith.ConstantOp(i32, 2) + y_false = arith.ConstantOp(i32, 3) + scf.YieldOp([x_false, y_false]) +add = arith.AddIOp(if_op.results[0], if_op.results[1]) +return + + +# CHECK: func @simple_if_else(%[[ARG0:.*]]: i1) +# CHECK: %[[RET:.*]]:2 = scf.if %[[ARG0:.*]] +# CHECK: %[[ZERO:.*]] = arith.constant 0 +# CHECK: %[[ONE:.*]] = arith.constant 1 +# CHECK: scf.yield %[[ZERO]], %[[ONE]] +# CHECK: } else { +# CHECK: %[[TWO:.*]] = arith.constant 2 +# CHECK: %[[THREE:.*]] = arith.constant 3 +# CHECK: scf.yield %[[TWO]], %[[THREE]] +# CHECK: arith.addi %[[RET]]#0, %[[RET]]#1 +# CHECK: return Index: mlir/test/python/dialects/scf.py === --- mlir/test/python/dialects/scf.py +++ mlir/test/python/dialects/scf.py @@ -82,3 +82,58 @@ # CHECK: iter_args(%{{.*}} = %[[ARGS]]#0, %{{.*}} = %[[ARGS]]#1) # CHECK: scf.yield %{{.*}}, %{{.*}} # CHECK: return + + +@constructAndPrintInModule +def testIfWithoutElse(): + bool = IntegerType.get_signless(1) + i32 = IntegerType.get_signless(32) + + @builtin.FuncOp.from_py_func(bool) + def simple_if(cond): +if_op = scf.IfOp(cond) +with InsertionPoint(if_op.then_block): + one = arith.ConstantOp(i32, 1) + add = arith.AddIOp(one, one) + scf.YieldOp([]) +return + + +# CHECK: func @simple_if(%[[ARG0:.*]]: i1) +# CHECK: scf.if %[[ARG0:.*]] +# CHECK: %[[ONE:.*]] = arith.constant 1 +# CHECK: %[[ADD:.*]] = arith.addi %[[ONE]], %[[ONE]] +# CHECK: return + + +@constructAndPrintInModule +def testIfWithElse(): + bool = IntegerType.get_signless(1) + i32 = IntegerType.get_signless(32) + + @builtin.FuncOp.from_py_func(bool) + def simple_if_else(cond): +if_op = scf.IfOp(cond, [i32, i32], hasElse=True) +with InsertionPoint(if_op.then_block): + x_true = arith.ConstantOp(i32, 0) + y_true = arith.ConstantOp(i32, 1) + scf.YieldOp([x_true, y_true]) +with InsertionPoint(if_op.else_block): + x_false = arith.ConstantOp(i32, 2) + y_false = arith.ConstantOp(i32, 3) + scf.YieldOp([x_false, y_false]) +add = arith.AddIOp(if_op.results[0], if_op.results[1]) +return + + +# CHECK: func @simple_if_else(%[[ARG0:.*]]: i1) +# CHECK: %[[RET:.*]]:2 = scf.if %[[ARG0:.*]] +# CHECK: %[[ZERO:.*]] = arith.constant 0 +# CHECK: %[[ONE:.*]] = arith.constant 1 +# CHECK: scf.yield %[[ZERO]], %[[ONE]] +# CHECK: } else { +# CHECK: %[[TWO:.*]] = arith.constant 2 +# CHECK: %[[THREE:.*]] = arith.constant 3 +# CHECK: scf.yield %[[TWO]], %[[THREE]] +# CHECK: arith.addi %[[RET]]#0, %[[RET]]#1 +# CHECK: return ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D121076: [MLIR][Python] Add SCFIfOp Python binding
chhzh123 updated this revision to Diff 413520. chhzh123 added a comment. Revert Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D121076/new/ https://reviews.llvm.org/D121076 Files: mlir/python/mlir/dialects/_scf_ops_ext.py Index: mlir/python/mlir/dialects/_scf_ops_ext.py === --- mlir/python/mlir/dialects/_scf_ops_ext.py +++ mlir/python/mlir/dialects/_scf_ops_ext.py @@ -64,3 +64,44 @@ To obtain the loop-carried operands, use `iter_args`. """ return self.body.arguments[1:] + + +class IfOp: + """Specialization for the SCF if op class.""" + + def __init__(self, + results_, + cond, + withElseRegion=False, + *, + loc=None, + ip=None): +"""Creates an SCF `if` operation. + +- `cond` is a boolean value to determine which regions of code will be executed. +- `withElseRegion` determines whether the if operation has the else branch. +""" +operands = [] +operands.append(cond) +results = [] +results.extend(results_) +super().__init__( +self.build_generic( +regions=2, +results=results, +operands=operands, +loc=loc, +ip=ip)) +self.regions[0].blocks.append(*results) +if withElseRegion: +self.regions[1].blocks.append(*results) + + @property + def then_block(self): +"""Returns the then block of the if operation.""" +return self.regions[0].blocks[0] + + @property + def else_block(self): +"""Returns the else block of the if operation.""" +return self.regions[1].blocks[0] Index: mlir/python/mlir/dialects/_scf_ops_ext.py === --- mlir/python/mlir/dialects/_scf_ops_ext.py +++ mlir/python/mlir/dialects/_scf_ops_ext.py @@ -64,3 +64,44 @@ To obtain the loop-carried operands, use `iter_args`. """ return self.body.arguments[1:] + + +class IfOp: + """Specialization for the SCF if op class.""" + + def __init__(self, + results_, + cond, + withElseRegion=False, + *, + loc=None, + ip=None): +"""Creates an SCF `if` operation. + +- `cond` is a boolean value to determine which regions of code will be executed. +- `withElseRegion` determines whether the if operation has the else branch. +""" +operands = [] +operands.append(cond) +results = [] +results.extend(results_) +super().__init__( +self.build_generic( +regions=2, +results=results, +operands=operands, +loc=loc, +ip=ip)) +self.regions[0].blocks.append(*results) +if withElseRegion: +self.regions[1].blocks.append(*results) + + @property + def then_block(self): +"""Returns the then block of the if operation.""" +return self.regions[0].blocks[0] + + @property + def else_block(self): +"""Returns the else block of the if operation.""" +return self.regions[1].blocks[0] ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D121076: [MLIR][Python] Add SCFIfOp Python binding
chhzh123 added a comment. Oh seems I made a wrong merge from the master branch. Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D121076/new/ https://reviews.llvm.org/D121076 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D121076: [MLIR][Python] Add SCFIfOp Python binding
chhzh123 updated this revision to Diff 413518. chhzh123 added a comment. Herald added subscribers: cfe-commits, llvm-commits, libc-commits, libcxx-commits, pcwang-thead, awarzynski, arjunp, luke957, abrachet, Groverkss, ormris, foad, mravishankar, frasercrmck, dexonsmith, ecnelises, ThomasRaoux, okura, jdoerfert, sstefan1, kuter, martong, phosek, kerbowa, csigg, luismarques, apazos, sameer.abuasal, pengfei, s.egerton, dmgreen, Jim, asbirlea, jocewei, PkmX, arphaman, the_o, brucehoult, MartinMosbeck, rogfer01, edward-jones, zzheng, MaskRay, jrtc27, niosHD, cryptoad, sabuasal, simoncook, johnrusso, rbar, asb, kbarton, aheejin, hiraditya, arichardson, mgorny, nhaehnle, jvesely, nemanjai, arsenm. Herald added a reviewer: alexander-shaposhnikov. Herald added a reviewer: rupprecht. Herald added a reviewer: jdoerfert. Herald added a reviewer: jhenderson. Herald added a reviewer: jdoerfert. Herald added a reviewer: sstefan1. Herald added a reviewer: antiagainst. Herald added a reviewer: nicolasvasilache. Herald added a reviewer: herhut. Herald added a reviewer: rriddle. Herald added a reviewer: aartbik. Herald added a reviewer: sscalpone. Herald added a reviewer: jpienaar. Herald added a reviewer: baziotis. Herald added a reviewer: aartbik. Herald added a reviewer: aartbik. Herald added a reviewer: awarzynski. Herald added a reviewer: bondhugula. Herald added projects: clang, libc++, libc-project, LLVM, clang-tools-extra, Flang. Herald added a reviewer: libc++. Fix signature and add tests Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D121076/new/ https://reviews.llvm.org/D121076 Files: README.md clang-tools-extra/docs/clang-tidy/Contributing.rst clang/docs/ClangFormattedStatus.rst clang/docs/DataFlowAnalysisIntro.md clang/docs/analyzer/checkers.rst clang/docs/tools/clang-formatted-files.txt clang/include/clang/Basic/Module.h clang/include/clang/Parse/Parser.h clang/include/clang/Sema/DeclSpec.h clang/include/clang/Tooling/Syntax/Pseudo/DirectiveMap.h clang/include/clang/Tooling/Syntax/Pseudo/Preprocess.h clang/lib/Basic/Module.cpp clang/lib/CodeGen/CGBuiltin.cpp clang/lib/Format/UnwrappedLineFormatter.cpp clang/lib/Parse/ParseDecl.cpp clang/lib/Parse/ParseDeclCXX.cpp clang/lib/Parse/ParseExprCXX.cpp clang/lib/Parse/ParsePragma.cpp clang/lib/Parse/ParseStmt.cpp clang/lib/Parse/ParseTentative.cpp clang/lib/Serialization/ASTWriter.cpp clang/lib/StaticAnalyzer/Checkers/DeadStoresChecker.cpp clang/lib/StaticAnalyzer/Checkers/GenericTaintChecker.cpp clang/lib/Tooling/Syntax/Pseudo/CMakeLists.txt clang/lib/Tooling/Syntax/Pseudo/DirectiveMap.cpp clang/lib/Tooling/Syntax/Pseudo/Preprocess.cpp clang/lib/Tooling/Syntax/Pseudo/README.md clang/test/Analysis/taint-generic.c clang/test/CodeGen/PowerPC/builtins-ppc-fma.c clang/test/CodeGen/PowerPC/builtins-ppc-fpconstrained.c clang/test/CodeGen/PowerPC/builtins-ppc-vsx.c clang/test/CodeGen/PowerPC/builtins-ppc-xlcompat-math.c clang/test/Modules/Inputs/AddRemoveIrrelevantModuleMap/a.modulemap clang/test/Modules/Inputs/AddRemoveIrrelevantModuleMap/b.modulemap clang/test/Modules/add-remove-irrelevant-module-map.m clang/test/OpenMP/irbuilder_nested_openmp_parallel_empty.c clang/test/OpenMP/irbuilder_nested_parallel_for.c clang/test/Syntax/lex.c clang/tools/clang-pseudo/ClangPseudo.cpp clang/unittests/Format/FormatTest.cpp clang/unittests/Tooling/LookupTest.cpp clang/unittests/Tooling/Syntax/Pseudo/CMakeLists.txt clang/unittests/Tooling/Syntax/Pseudo/DirectiveMapTest.cpp clang/unittests/Tooling/Syntax/Pseudo/PreprocessTest.cpp flang/tools/bbc/bbc.cpp flang/tools/fir-opt/fir-opt.cpp flang/tools/tco/tco.cpp libc/loader/linux/x86_64/start.cpp libcxx/docs/ReleaseNotes.rst libcxx/include/CMakeLists.txt libcxx/include/__algorithm/ranges_max_element.h libcxx/include/__memory/unique_ptr.h libcxx/include/__ranges/access.h libcxx/include/algorithm libcxx/include/cuchar libcxx/include/module.modulemap libcxx/include/uchar.h libcxx/test/libcxx/clang_tidy.sh.cpp libcxx/test/libcxx/diagnostics/detail.headers/algorithm/ranges_max_element.module.verify.cpp libcxx/test/libcxx/double_include.sh.cpp libcxx/test/libcxx/min_max_macros.compile.pass.cpp libcxx/test/libcxx/nasty_macros.compile.pass.cpp libcxx/test/libcxx/no_assert_include.compile.pass.cpp libcxx/test/libcxx/strings/c.strings/version_cuchar.pass.cpp libcxx/test/std/algorithms/alg.sorting/alg.min.max/ranges.max_element.pass.cpp libcxx/test/std/depr/depr.c.headers/uchar_h.compile.pass.cpp libcxx/test/std/depr/depr.c.headers/uchar_h.pass.cpp libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp libcxx/test/std/ranges/range.access/begin.sizezero.pass.cpp libcxx/test/std/ranges/range.access/end.sizezero.pass.cpp libcxx/test/std/strings/c.strings/cuchar.compile.pass.cpp