Skip to content

Instantly share code, notes, and snippets.

@benvanik
Created April 3, 2020 17:52
Show Gist options
  • Save benvanik/c336d31de223db5efb90662c9770dc48 to your computer and use it in GitHub Desktop.
Save benvanik/c336d31de223db5efb90662c9770dc48 to your computer and use it in GitHub Desktop.
def HAL_DeviceSwitchOp : HAL_Op<"device.switch"> {
let summary = [{runtime device switch pseudo op}];
let description = [{
Switches between multiple regions based on the runtime device type.
The provided regions are pattern-matched against the runtime backend of the
given device and executed only when the device matches.
As the patterns can match on wildcards this enables conditions that have
similar bodies to be folded. The patterns themselves are only matched once
at startup and then the results are cached; the runtime overhead is
equivalent to a normal switch statement. In cases where the compiler can
statically identify the device type entire cases can be folded away.
Supported conditions:
* `#hal.match<...>`: execute the region if the expression matches.
* `default`: execute the region if no other expression matches.
Supported expressions:
* `any([...])`: matches if any of the nested expressions match.
* `all([...])`: matches only if all of the nested expressions match.
* `device("pattern*-?-*")`: matches against the device identifier.
The pattern is evaluated with standard file path wildcards (`*` for zero
or more characters and `?` for one character).
If more than one condition is satisfied the first listed will be chosen.
More specific conditions should be earlier in the set.
```mlir
%c0 = constant 0 : i32
%c1 = constant 1 : i32
%c2 = constant 2 : i32
%device = ... : !hal.device
%0 = hal.device.switch(%device : !hal.device) {
#hal.match<device("vulkan-v1.1*")>(%c1a = %c1 : i32) -> i32 {
hal.return %c1a : i32
}
#hal.match<any([device("vmla"), device("vulkan-*")])>(%c2a = %c2 : i32) -> i32 {
hal.return %c2a : i32
}
default(%c0a = %c0 : i32) -> i32 {
hal.return %c0a : i32
}
}
```
}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment