Skip to content

scf.for

scf dialect

Bounded counted loop over an index or offset domain with optional loop-carried state.

Operation contract

Property Value
Semantic phase source structure
Traits ImplicitTerminator(scf.yield)
Interfaces LoopLike

Signature

Kind Name Type Cardinality Description
Operand lower_bound address required Inclusive index or physical byte-offset lower bound.
Operand upper_bound address required Exclusive upper bound in the lower-bound address domain.
Operand step address required Positive step in the lower-bound address domain.
Operand iter_args any variadic
Operand unroll_factor index optional Optional SSA unroll factor policy consumed by unroll transforms. The factor bounds body cloning and does not require the loop trip count to be static.
Result results any variadic
Attribute unroll_policy enum ScfForUnrollPolicy optional Optional bare unroll policy for required full unroll.
Attribute unroll_schedule enum ScfForUnrollSchedule optional Optional schedule used when materializing unrolled loop body copies.
Region body region required Loop body. Terminated by scf.yield. (single block, terminator scf.yield.)

Verification constraints

  • SameType(lower_bound, upper_bound, step)
  • IterArgsMatchResults(iter_args, results)
  • YieldCountMatchesResults(body, results)
  • YieldTypesMatchResults(body, results)

Examples

scf.for %iv = [%c0 to %n step %c1] {
  scf.yield
}
scf.for %byte_offset = [%zero to %byte_length step %one] {
  scf.yield
}
%result = scf.for %iv = [%c0 to %n step %c1](%acc = %init : f32) -> (f32) {
  %next = scalar.addf %acc, %acc : f32
  scf.yield %next : f32
}
scf.for %iv = [%c0 to %n step %c1] unroll(%factor) {
  scf.yield
}
scf.for %iv = [%c0 to %n step %c1] unroll(%factor) schedule(interleaved) {
  scf.yield
}
%result = scf.for %iv = [%c0 to %n step %c1](%acc = %init : f32) -> (f32) unroll(%factor) schedule(recurrence) {
  %next = scalar.addf %acc, %acc : f32
  scf.yield %next : f32
}