} { ... }
```
-Dialect conversion framework will utilize the information in `spv.target_env`
-to properly filter out patterns and ops not available in the target execution
-environment.
+Dialect conversion framework will utilize the information in `spv.target_env` to
+properly filter out patterns and ops not available in the target execution
+environment. When targeting SPIR-V, one needs to create a
+[`SPIRVConversionTarget`](#spirvconversiontarget) by providing such an
+attribute.
## Shader interface (ABI)
proper hooks to check the dynamic legality of SPIR-V ops. Users can further
register other legality constraints into the returned `SPIRVConversionTarget`.
+`spirv::lookupTargetEnvOrDefault()` is a handy utility function to query an
+`spv.target_env` attached in the input IR or use the feault to construct a
+`SPIRVConversionTarget`.
+
### `SPIRVTypeConverter`
The `mlir::SPIRVTypeConverter` derives from `mlir::TypeConverter` and provides
/// and no extra extensions.
TargetEnvAttr getDefaultTargetEnv(MLIRContext *context);
-/// Queries the target environment from the given `op` or returns the default
-/// target environment (SPIR-V 1.0 with Shader capability and no extra
-/// extensions) if not provided.
+/// Queries the target environment recursively from enclosing symbol table ops
+/// containing the given `op` or returns the default target environment as
+/// returned by getDefaultTargetEnv() if not provided.
TargetEnvAttr lookupTargetEnvOrDefault(Operation *op);
} // namespace spirv
#include "mlir/IR/Builders.h"
#include "mlir/IR/FunctionSupport.h"
#include "mlir/IR/Operation.h"
+#include "mlir/IR/SymbolTable.h"
using namespace mlir;
}
spirv::TargetEnvAttr spirv::lookupTargetEnvOrDefault(Operation *op) {
- if (auto attr = op->getAttrOfType<spirv::TargetEnvAttr>(
- spirv::getTargetEnvAttrName()))
- return attr;
+ Operation *symTable = op;
+ while (symTable) {
+ symTable = SymbolTable::getNearestSymbolTable(symTable);
+ if (!symTable)
+ break;
+
+ if (auto attr = symTable->getAttrOfType<spirv::TargetEnvAttr>(
+ spirv::getTargetEnvAttrName()))
+ return attr;
+
+ symTable = symTable->getParentOp();
+ }
+
return getDefaultTargetEnv(op->getContext());
}