def testBlockContextAppend(self): with self.module.function_context("foo", [], []) as fun: E.constant_index(41) with E.BlockContext() as b: blk = b # save block handle for later E.constant_index(0) E.constant_index(42) with E.BlockContext(E.appendTo(blk)): E.constant_index(1) code = str(fun) # Find positions of instructions and make sure they are in the block we put # them by comparing those positions. c41pos = code.find("%c41 = constant 41 : index") c42pos = code.find("%c42 = constant 42 : index") bb1pos = code.find("^bb1:") c0pos = code.find("%c0 = constant 0 : index") c1pos = code.find("%c1 = constant 1 : index") self.assertNotEqual(c41pos, -1) self.assertNotEqual(c42pos, -1) self.assertNotEqual(bb1pos, -1) self.assertNotEqual(c0pos, -1) self.assertNotEqual(c1pos, -1) self.assertGreater(bb1pos, c41pos) self.assertGreater(bb1pos, c42pos) self.assertLess(bb1pos, c0pos) self.assertLess(bb1pos, c1pos)
def testCondBr(self): self.setUp() with self.module.function_context("foo", [self.boolType], []) as fun: with E.BlockContext() as blk1: E.ret([]) with E.BlockContext([self.indexType]) as blk2: E.ret([]) cst = E.constant_index(0) E.cond_br(fun.arg(0), blk1, [], blk2, [cst]) printWithCurrentFunctionName(str(fun))
def testCondBr(self): with self.module.function_context("foo", [self.boolType], []) as fun: with E.BlockContext() as blk1: E.ret([]) with E.BlockContext([self.indexType]) as blk2: E.ret([]) cst = E.constant_index(0) E.cond_br(fun.arg(0), blk1, [], blk2, [cst]) code = str(fun) self.assertIn("cond_br %arg0, ^bb1, ^bb2(%c0 : index)", code)
def testBlockContextAppend(self): self.setUp() with self.module.function_context("foo", [], []) as fun: E.constant_index(41) with E.BlockContext() as b: blk = b # save block handle for later E.constant_index(0) E.constant_index(42) with E.BlockContext(E.appendTo(blk)): E.constant_index(1) printWithCurrentFunctionName(str(fun))
def testBlockContextStandalone(self): self.setUp() with self.module.function_context("foo", [], []) as fun: blk1 = E.BlockContext() blk2 = E.BlockContext() with blk1: E.constant_index(0) with blk2: E.constant_index(56) E.constant_index(57) E.constant_index(41) with blk1: E.constant_index(1) E.constant_index(42) printWithCurrentFunctionName(str(fun))
def testBlockContext(self): self.setUp() with self.module.function_context("foo", [], []) as fun: cst = E.constant_index(42) with E.BlockContext(): cst + cst printWithCurrentFunctionName(str(fun))
def testBlockArguments(self): self.setUp() with self.module.function_context("foo", [], []) as fun: E.constant_index(42) with E.BlockContext([self.f32Type, self.f32Type]) as b: b.arg(0) + b.arg(1) printWithCurrentFunctionName(str(fun))
def testBrDeclaration(self): self.setUp() with self.module.function_context("foo", [], []) as fun: blk = E.BlockContext() E.br(blk.handle()) with blk: E.ret() printWithCurrentFunctionName(str(fun))
def testBrArgs(self): self.setUp() with self.module.function_context("foo", [], []) as fun: # Create an infinite loop. with E.BlockContext([self.indexType, self.indexType]) as b: E.br(b, [b.arg(1), b.arg(0)]) E.br(b, [E.constant_index(0), E.constant_index(1)]) printWithCurrentFunctionName(str(fun))
def testBr(self): self.setUp() with self.module.function_context("foo", [], []) as fun: with E.BlockContext() as b: blk = b E.ret() E.br(blk) printWithCurrentFunctionName(str(fun))
def testBlockArguments(self): with self.module.function_context("foo", [], []) as fun: E.constant_index(42) with E.BlockContext([self.f32Type, self.f32Type]) as b: b.arg(0) + b.arg(1) code = str(fun) self.assertIn("%c42 = constant 42 : index", code) self.assertIn("^bb1(%0: f32, %1: f32):", code) self.assertIn(" %2 = addf %0, %1 : f32", code)
def testBrDeclaration(self): with self.module.function_context("foo", [], []) as fun: blk = E.BlockContext() E.br(blk.handle()) with blk: E.ret() code = str(fun) self.assertIn(" br ^bb1", code) self.assertIn("^bb1:", code) self.assertIn(" return", code)
def testBr(self): with self.module.function_context("foo", [], []) as fun: with E.BlockContext() as b: blk = b E.ret() E.br(blk) code = str(fun) self.assertIn(" br ^bb1", code) self.assertIn("^bb1:", code) self.assertIn(" return", code)
def testBlockContextStandalone(self): with self.module.function_context("foo", [], []) as fun: blk1 = E.BlockContext() blk2 = E.BlockContext() with blk1: E.constant_index(0) with blk2: E.constant_index(56) E.constant_index(57) E.constant_index(41) with blk1: E.constant_index(1) E.constant_index(42) code = str(fun) # Find positions of instructions and make sure they are in the block we put # them by comparing those positions. c41pos = code.find(" %c41 = constant 41 : index") c42pos = code.find(" %c42 = constant 42 : index") bb1pos = code.find("^bb1:") c0pos = code.find(" %c0 = constant 0 : index") c1pos = code.find(" %c1 = constant 1 : index") bb2pos = code.find("^bb2:") c56pos = code.find(" %c56 = constant 56 : index") c57pos = code.find(" %c57 = constant 57 : index") self.assertNotEqual(c41pos, -1) self.assertNotEqual(c42pos, -1) self.assertNotEqual(bb1pos, -1) self.assertNotEqual(c0pos, -1) self.assertNotEqual(c1pos, -1) self.assertNotEqual(bb2pos, -1) self.assertNotEqual(c56pos, -1) self.assertNotEqual(c57pos, -1) self.assertGreater(bb1pos, c41pos) self.assertGreater(bb1pos, c42pos) self.assertLess(bb1pos, c0pos) self.assertLess(bb1pos, c1pos) self.assertGreater(bb2pos, c0pos) self.assertGreater(bb2pos, c1pos) self.assertGreater(bb2pos, bb1pos) self.assertLess(bb2pos, c56pos) self.assertLess(bb2pos, c57pos)
def testBrArgs(self): with self.module.function_context("foo", [], []) as fun: # Create an infinite loop. with E.BlockContext([self.indexType, self.indexType]) as b: E.br(b, [b.arg(1), b.arg(0)]) E.br(b, [E.constant_index(0), E.constant_index(1)]) code = str(fun) self.assertIn(" %c0 = constant 0 : index", code) self.assertIn(" %c1 = constant 1 : index", code) self.assertIn(" br ^bb1(%c0, %c1 : index, index)", code) self.assertIn("^bb1(%0: index, %1: index):", code) self.assertIn(" br ^bb1(%1, %0 : index, index)", code)
def testBlockContext(self): with self.module.function_context("foo", [], []) as fun: cst = E.constant_index(42) with E.BlockContext(): cst + cst code = str(fun) # Find positions of instructions and make sure they are in the block we # put them by comparing those positions. # TODO(zinenko,ntv): this (and tests below) should use FileCheck instead. c42pos = code.find("%c42 = constant 42 : index") bb1pos = code.find("^bb1:") c84pos = code.find( '%0 = "affine.apply"() {map: () -> (84)} : () -> index') self.assertNotEqual(c42pos, -1) self.assertNotEqual(bb1pos, -1) self.assertNotEqual(c84pos, -1) self.assertGreater(bb1pos, c42pos) self.assertLess(bb1pos, c84pos)