Skip to content

Instantly share code, notes, and snippets.

@antiagainst
Last active September 7, 2022 18:22
Show Gist options
  • Save antiagainst/7a3664afa519baa443b10bad4e6282b9 to your computer and use it in GitHub Desktop.
Save antiagainst/7a3664afa519baa443b10bad4e6282b9 to your computer and use it in GitHub Desktop.
// Option#1: use sequence of blocks
// Missing all goodies for regions :(
// Option#2: use scf dialect for scf
// Missing story inside spirv; hard to deserialize from blob :(
// Option#3: flesh out spirv scf ops: introduce new terminators:
// - spv.mlir.fallthrough // jump to the next region
// - spv.mlir.condition // jump to the next region if true; jump out of the current scf op if false
// - spv.mlir.merge // jump out of the current scf op
// - spv.mlir.reenter // jump back to the current scf op's header region
// - spv.mlir.break // jump out of the parent scf op
// - spv.mlir.continue // jump to the parent scf op's continue region
// Design considerations:
// * How easy to convert from HLSL
// * How easy to convert to SPIR-V proper
// * How easy to convert from SPIR-V proper
// * How resilient w.r.t. transformations
// condition
if (condition) {
} else {
}
spv.mlir.selection (%condition)
then = {
// ...
spv.mlir.merge
}
else = {
// ...
spv.mlir.merge
}
// switch
switch (selector) {
case case1: /* ... */ break;
case case2: /* ... */ // fallthrough;
default: ...
}
spv.mlir.switch (%selector)
case1 = {
// ...
spv.mlir.merge
}
case2 = {
// ...
spv.mlir.fallthrough
}
default = {
// ...
spv.mlir.merge
}
// loop
while (cond) {
}
spv.mlir.loop
header = {
// Compute condition
spv.mlir.condition(%condition)
}
body = {
// Main logic
spv.mlir.fallthrough // to continue region
}
continue = {
// Nothing else
spv.mlir.reenter // back to header region
}
do {
} while (cond);
spv.mlir.loop
header = {
// Main logic
// Compute condition
spv.mlir.condition(%condition)
}
body = {
// Nothing else
spv.mlir.fallthrough // to continue region
}
continue = {
// Nothing else
spv.mlir.reenter // back to header region
}
for (int i = 0; i < N; ++i) {
}
spv.mlir.loop
header = {
// Compute condition: i < N
spv.mlir.condition(%condition)
}
body = {
// Main logic
spv.mlir.fallthrough // to continue region
}
continue = {
// ++i
spv.mlir.reenter // back to header region
}
// Cases
for(int i = 0; i < 10; ++i) {
if (cond) {
foo();
continue;
}
}
spv.mlir.loop
header = {
// Compute condition: i < 10
spv.mlir.condition(%condition)
}
body = {
spv.mlir.selection (%condition)
then = {
// foo()
spv.mlir.continue // to continue region
}
else = {
}
spv.mlir.fallthrough // to continue region
}
continue = {
// ++i
spv.mlir.reenter // back to header region
}
// phi: using block arguments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment