| 1 |
// Copyright © 2022 Collabora, Ltd. |
| 2 |
// SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
#![allow(non_upper_case_globals)] |
| 5 |
|
| 6 |
use crate::api::GetDebugFlags; |
| 7 |
use crate::api::DEBUG; |
| 8 |
use crate::builder::*; |
| 9 |
use crate::ir::*; |
| 10 |
use crate::sph::{OutputTopology, PixelImap}; |
| 11 |
|
| 12 |
use nak_bindings::*; |
| 13 |
|
| 14 |
use compiler::bindings::*; |
| 15 |
use compiler::cfg::CFGBuilder; |
| 16 |
use compiler::nir::*; |
| 17 |
use compiler::nir_instr_printer::NirInstrPrinter; |
| 18 |
use rustc_hash::{FxBuildHasher, FxHashMap, FxHashSet}; |
| 19 |
use std::cmp::max; |
| 20 |
use std::ops::Index; |
| 21 |
|
| 22 |
fn init_info_from_nir(nak: &nak_compiler, nir: &nir_shader) -> ShaderInfo { |
| 23 |
ShaderInfo { |
| 24 |
max_warps_per_sm: 0, |
| 25 |
num_gprs: 0, |
| 26 |
num_instrs: 0, |
| 27 |
num_static_cycles: 0, |
| 28 |
num_spills_to_mem: 0, |
| 29 |
num_fills_from_mem: 0, |
| 30 |
num_spills_to_reg: 0, |
| 31 |
num_fills_from_reg: 0, |
| 32 |
num_control_barriers: 0, |
| 33 |
slm_size: nir.scratch_size, |
| 34 |
max_crs_depth: 0, |
| 35 |
uses_global_mem: false, |
| 36 |
writes_global_mem: false, |
| 37 |
// TODO: handle this. |
| 38 |
uses_fp64: false, |
| 39 |
stage: match nir.info.stage() { |
| 40 |
MESA_SHADER_COMPUTE => { |
| 41 |
ShaderStageInfo::Compute(ComputeShaderInfo { |
| 42 |
local_size: [ |
| 43 |
nir.info.workgroup_size[0], |
| 44 |
nir.info.workgroup_size[1], |
| 45 |
nir.info.workgroup_size[2], |
| 46 |
], |
| 47 |
smem_size: nir.info.shared_size.try_into().unwrap(), |
| 48 |
}) |
| 49 |
} |
| 50 |
MESA_SHADER_VERTEX => ShaderStageInfo::Vertex(VertexShaderInfo { |
| 51 |
isbe_space_sharing_enable: false, |
| 52 |
}), |
| 53 |
MESA_SHADER_FRAGMENT => { |
| 54 |
let info_fs = unsafe { &nir.info.__bindgen_anon_1.fs }; |
| 55 |
ShaderStageInfo::Fragment(FragmentShaderInfo { |
| 56 |
uses_kill: false, |
| 57 |
does_interlock: false, |
| 58 |
post_depth_coverage: info_fs.post_depth_coverage(), |
| 59 |
early_fragment_tests: info_fs.early_fragment_tests(), |
| 60 |
uses_sample_shading: info_fs.uses_sample_shading(), |
| 61 |
}) |
| 62 |
} |
| 63 |
MESA_SHADER_GEOMETRY => { |
| 64 |
let info_gs = unsafe { &nir.info.__bindgen_anon_1.gs }; |
| 65 |
let output_topology = match info_gs.output_primitive { |
| 66 |
MESA_PRIM_POINTS => OutputTopology::PointList, |
| 67 |
MESA_PRIM_LINE_STRIP => OutputTopology::LineStrip, |
| 68 |
MESA_PRIM_TRIANGLE_STRIP => OutputTopology::TriangleStrip, |
| 69 |
_ => panic!( |
| 70 |
"Invalid GS input primitive {}", |
| 71 |
info_gs.input_primitive |
| 72 |
), |
| 73 |
}; |
| 74 |
|
| 75 |
ShaderStageInfo::Geometry(GeometryShaderInfo { |
| 76 |
// TODO: Should be set if VK_NV_geometry_shader_passthrough is in use. |
| 77 |
passthrough_enable: false, |
| 78 |
stream_out_mask: info_gs.active_stream_mask(), |
| 79 |
threads_per_input_primitive: info_gs.invocations, |
| 80 |
output_topology: output_topology, |
| 81 |
max_output_vertex_count: info_gs.vertices_out, |
| 82 |
}) |
| 83 |
} |
| 84 |
MESA_SHADER_TESS_CTRL => { |
| 85 |
let info_tess = unsafe { &nir.info.__bindgen_anon_1.tess }; |
| 86 |
ShaderStageInfo::TessellationInit(TessellationInitShaderInfo { |
| 87 |
per_patch_attribute_count: 6, |
| 88 |
threads_per_patch: info_tess.tcs_vertices_out, |
| 89 |
}) |
| 90 |
} |
| 91 |
MESA_SHADER_TESS_EVAL => { |
| 92 |
let info_tess = unsafe { &nir.info.__bindgen_anon_1.tess }; |
| 93 |
ShaderStageInfo::Tessellation(TessellationShaderInfo { |
| 94 |
domain: match info_tess._primitive_mode { |
| 95 |
TESS_PRIMITIVE_TRIANGLES => { |
| 96 |
TessellationDomain::Triangle |
| 97 |
} |
| 98 |
TESS_PRIMITIVE_QUADS => TessellationDomain::Quad, |
| 99 |
TESS_PRIMITIVE_ISOLINES => TessellationDomain::Isoline, |
| 100 |
_ => panic!("Invalid tess_primitive_mode"), |
| 101 |
}, |
| 102 |
spacing: match info_tess.spacing() { |
| 103 |
TESS_SPACING_EQUAL => TessellationSpacing::Integer, |
| 104 |
TESS_SPACING_FRACTIONAL_ODD => { |
| 105 |
TessellationSpacing::FractionalOdd |
| 106 |
} |
| 107 |
TESS_SPACING_FRACTIONAL_EVEN => { |
| 108 |
TessellationSpacing::FractionalEven |
| 109 |
} |
| 110 |
_ => panic!("Invalid gl_tess_spacing"), |
| 111 |
}, |
| 112 |
primitives: if info_tess.point_mode() { |
| 113 |
TessellationPrimitives::Points |
| 114 |
} else if info_tess._primitive_mode |
| 115 |
== TESS_PRIMITIVE_ISOLINES |
| 116 |
{ |
| 117 |
TessellationPrimitives::Lines |
| 118 |
} else if info_tess.ccw() { |
| 119 |
TessellationPrimitives::TrianglesCCW |
| 120 |
} else { |
| 121 |
TessellationPrimitives::TrianglesCW |
| 122 |
}, |
| 123 |
}) |
| 124 |
} |
| 125 |
_ => panic!("Unknown shader stage"), |
| 126 |
}, |
| 127 |
io: match nir.info.stage() { |
| 128 |
MESA_SHADER_COMPUTE => ShaderIoInfo::None, |
| 129 |
MESA_SHADER_FRAGMENT => ShaderIoInfo::Fragment(FragmentIoInfo { |
| 130 |
sysvals_in: SysValInfo { |
| 131 |
// Required on fragment shaders, otherwise it cause a trap. |
| 132 |
ab: 1 << 31, |
| 133 |
c: 0, |
| 134 |
}, |
| 135 |
sysvals_in_d: [PixelImap::Unused; 8], |
| 136 |
attr_in: [PixelImap::Unused; 128], |
| 137 |
barycentric_attr_in: [0; 4], |
| 138 |
reads_sample_mask: false, |
| 139 |
writes_color: 0, |
| 140 |
writes_sample_mask: false, |
| 141 |
writes_depth: false, |
| 142 |
}), |
| 143 |
MESA_SHADER_VERTEX |
| 144 |
| MESA_SHADER_GEOMETRY |
| 145 |
| MESA_SHADER_TESS_CTRL |
| 146 |
| MESA_SHADER_TESS_EVAL => { |
| 147 |
let num_clip = nir.info.clip_distance_array_size(); |
| 148 |
let num_cull = nir.info.cull_distance_array_size(); |
| 149 |
let clip_enable = (1_u32 << num_clip) - 1; |
| 150 |
let cull_enable = ((1_u32 << num_cull) - 1) << num_clip; |
| 151 |
|
| 152 |
ShaderIoInfo::Vtg(VtgIoInfo { |
| 153 |
sysvals_in: SysValInfo::default(), |
| 154 |
sysvals_in_d: 0, |
| 155 |
sysvals_out: SysValInfo::default(), |
| 156 |
sysvals_out_d: 0, |
| 157 |
attr_in: [0; 4], |
| 158 |
attr_out: [0; 4], |
| 159 |
|
| 160 |
// TODO: figure out how to fill this. |
| 161 |
store_req_start: u8::MAX, |
| 162 |
store_req_end: 0, |
| 163 |
|
| 164 |
clip_enable: clip_enable.try_into().unwrap(), |
| 165 |
cull_enable: cull_enable.try_into().unwrap(), |
| 166 |
xfb: if nir.xfb_info.is_null() { |
| 167 |
None |
| 168 |
} else { |
| 169 |
Some(Box::new(unsafe { |
| 170 |
nak_xfb_from_nir(nak, nir.xfb_info) |
| 171 |
})) |
| 172 |
}, |
| 173 |
}) |
| 174 |
} |
| 175 |
_ => panic!("Unknown shader stage"), |
| 176 |
}, |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
fn alloc_ssa_for_nir(b: &mut impl SSABuilder, ssa: &nir_def) -> Vec<SSAValue> { |
| 181 |
let (file, comps) = if ssa.bit_size == 1 { |
| 182 |
(RegFile::Pred, ssa.num_components) |
| 183 |
} else { |
| 184 |
let bits = ssa.bit_size * ssa.num_components; |
| 185 |
(RegFile::GPR, bits.div_ceil(32)) |
| 186 |
}; |
| 187 |
|
| 188 |
let mut vec = Vec::new(); |
| 189 |
for _ in 0..comps { |
| 190 |
vec.push(b.alloc_ssa(file)); |
| 191 |
} |
| 192 |
vec |
| 193 |
} |
| 194 |
|
| 195 |
struct PhiAllocMap<'a> { |
| 196 |
alloc: &'a mut PhiAllocator, |
| 197 |
map: FxHashMap<(u32, u8), Phi>, |
| 198 |
} |
| 199 |
|
| 200 |
impl<'a> PhiAllocMap<'a> { |
| 201 |
fn new(alloc: &'a mut PhiAllocator) -> PhiAllocMap<'a> { |
| 202 |
PhiAllocMap { |
| 203 |
alloc: alloc, |
| 204 |
map: Default::default(), |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
fn get_phi(&mut self, nphi: &nir_phi_instr, comp: u8) -> Phi { |
| 209 |
*self |
| 210 |
.map |
| 211 |
.entry((nphi.def.index, comp)) |
| 212 |
.or_insert_with(|| self.alloc.alloc()) |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
struct PerSizeFloatControls { |
| 217 |
pub ftz: bool, |
| 218 |
pub rnd_mode: FRndMode, |
| 219 |
} |
| 220 |
|
| 221 |
struct ShaderFloatControls { |
| 222 |
pub fp16: PerSizeFloatControls, |
| 223 |
pub fp32: PerSizeFloatControls, |
| 224 |
pub fp64: PerSizeFloatControls, |
| 225 |
} |
| 226 |
|
| 227 |
impl Default for ShaderFloatControls { |
| 228 |
fn default() -> Self { |
| 229 |
Self { |
| 230 |
fp16: PerSizeFloatControls { |
| 231 |
ftz: false, |
| 232 |
rnd_mode: FRndMode::NearestEven, |
| 233 |
}, |
| 234 |
fp32: PerSizeFloatControls { |
| 235 |
ftz: true, // Default FTZ on fp32 |
| 236 |
rnd_mode: FRndMode::NearestEven, |
| 237 |
}, |
| 238 |
fp64: PerSizeFloatControls { |
| 239 |
ftz: false, |
| 240 |
rnd_mode: FRndMode::NearestEven, |
| 241 |
}, |
| 242 |
} |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
impl ShaderFloatControls { |
| 247 |
fn from_nir(nir: &nir_shader) -> ShaderFloatControls { |
| 248 |
let nir_fc = nir.info.float_controls_execution_mode; |
| 249 |
let mut fc: ShaderFloatControls = Default::default(); |
| 250 |
|
| 251 |
if (nir_fc & FLOAT_CONTROLS_DENORM_PRESERVE_FP16) != 0 { |
| 252 |
fc.fp16.ftz = false; |
| 253 |
} else if (nir_fc & FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16) != 0 { |
| 254 |
fc.fp16.ftz = true; |
| 255 |
} |
| 256 |
if (nir_fc & FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP16) != 0 { |
| 257 |
fc.fp16.rnd_mode = FRndMode::NearestEven; |
| 258 |
} else if (nir_fc & FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16) != 0 { |
| 259 |
fc.fp16.rnd_mode = FRndMode::Zero; |
| 260 |
} |
| 261 |
|
| 262 |
if (nir_fc & FLOAT_CONTROLS_DENORM_PRESERVE_FP32) != 0 { |
| 263 |
fc.fp32.ftz = false; |
| 264 |
} else if (nir_fc & FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP32) != 0 { |
| 265 |
fc.fp32.ftz = true; |
| 266 |
} |
| 267 |
if (nir_fc & FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP32) != 0 { |
| 268 |
fc.fp32.rnd_mode = FRndMode::NearestEven; |
| 269 |
} else if (nir_fc & FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32) != 0 { |
| 270 |
fc.fp32.rnd_mode = FRndMode::Zero; |
| 271 |
} |
| 272 |
|
| 273 |
if (nir_fc & FLOAT_CONTROLS_DENORM_PRESERVE_FP64) != 0 { |
| 274 |
fc.fp64.ftz = false; |
| 275 |
} else if (nir_fc & FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP64) != 0 { |
| 276 |
fc.fp64.ftz = true; |
| 277 |
} |
| 278 |
if (nir_fc & FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP64) != 0 { |
| 279 |
fc.fp64.rnd_mode = FRndMode::NearestEven; |
| 280 |
} else if (nir_fc & FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64) != 0 { |
| 281 |
fc.fp64.rnd_mode = FRndMode::Zero; |
| 282 |
} |
| 283 |
|
| 284 |
fc |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
fn f_rnd_mode_from_nir(mode: nir_rounding_mode) -> FRndMode { |
| 289 |
match mode { |
| 290 |
nir_rounding_mode_undef | nir_rounding_mode_rtne => { |
| 291 |
FRndMode::NearestEven |
| 292 |
} |
| 293 |
nir_rounding_mode_ru => FRndMode::PosInf, |
| 294 |
nir_rounding_mode_rd => FRndMode::NegInf, |
| 295 |
nir_rounding_mode_rtz => FRndMode::Zero, |
| 296 |
_ => panic!("Invalid NIR rounding mode"), |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
impl Index<FloatType> for ShaderFloatControls { |
| 301 |
type Output = PerSizeFloatControls; |
| 302 |
|
| 303 |
fn index(&self, idx: FloatType) -> &PerSizeFloatControls { |
| 304 |
match idx { |
| 305 |
FloatType::F16 => &self.fp16, |
| 306 |
FloatType::F32 => &self.fp32, |
| 307 |
FloatType::F64 => &self.fp64, |
| 308 |
} |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
#[derive(Clone, Copy, Eq, Hash, PartialEq)] |
| 313 |
enum SyncType { |
| 314 |
Sync, |
| 315 |
Brk, |
| 316 |
Cont, |
| 317 |
} |
| 318 |
|
| 319 |
struct ShaderFromNir<'a> { |
| 320 |
nir: &'a nir_shader, |
| 321 |
sm: &'a ShaderModelInfo, |
| 322 |
info: ShaderInfo, |
| 323 |
float_ctl: ShaderFloatControls, |
| 324 |
cfg: CFGBuilder<u32, BasicBlock, FxBuildHasher>, |
| 325 |
label_alloc: LabelAllocator, |
| 326 |
block_label: FxHashMap<u32, Label>, |
| 327 |
bar_label: FxHashMap<u32, Label>, |
| 328 |
sync_blocks: FxHashSet<u32>, |
| 329 |
crs: Vec<(u32, SyncType)>, |
| 330 |
fs_out_regs: [Option<SSAValue>; 34], |
| 331 |
end_block_id: u32, |
| 332 |
ssa_map: FxHashMap<u32, Vec<SSAValue>>, |
| 333 |
saturated: FxHashSet<*const nir_def>, |
| 334 |
nir_instr_printer: NirInstrPrinter, |
| 335 |
} |
| 336 |
|
| 337 |
impl<'a> ShaderFromNir<'a> { |
| 338 |
fn new( |
| 339 |
nak: &nak_compiler, |
| 340 |
nir: &'a nir_shader, |
| 341 |
sm: &'a ShaderModelInfo, |
| 342 |
) -> Self { |
| 343 |
Self { |
| 344 |
nir: nir, |
| 345 |
sm: sm, |
| 346 |
info: init_info_from_nir(nak, nir), |
| 347 |
float_ctl: ShaderFloatControls::from_nir(nir), |
| 348 |
cfg: CFGBuilder::new(), |
| 349 |
label_alloc: LabelAllocator::new(), |
| 350 |
block_label: Default::default(), |
| 351 |
bar_label: Default::default(), |
| 352 |
sync_blocks: Default::default(), |
| 353 |
crs: Vec::new(), |
| 354 |
fs_out_regs: [None; 34], |
| 355 |
end_block_id: 0, |
| 356 |
ssa_map: Default::default(), |
| 357 |
saturated: Default::default(), |
| 358 |
nir_instr_printer: NirInstrPrinter::new().unwrap(), |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
fn get_block_label(&mut self, block: &nir_block) -> Label { |
| 363 |
*self |
| 364 |
.block_label |
| 365 |
.entry(block.index) |
| 366 |
.or_insert_with(|| self.label_alloc.alloc()) |
| 367 |
} |
| 368 |
|
| 369 |
fn push_crs(&mut self, target: &nir_block, sync_type: SyncType) { |
| 370 |
self.sync_blocks.insert(target.index); |
| 371 |
self.crs.push((target.index, sync_type)); |
| 372 |
let crs_depth = u32::try_from(self.crs.len()).unwrap(); |
| 373 |
self.info.max_crs_depth = max(self.info.max_crs_depth, crs_depth); |
| 374 |
} |
| 375 |
|
| 376 |
fn pop_crs(&mut self, target: &nir_block, sync_type: SyncType) { |
| 377 |
if let Some((top_index, top_sync_type)) = self.crs.pop() { |
| 378 |
assert!(top_index == target.index); |
| 379 |
assert!(top_sync_type == sync_type); |
| 380 |
} else { |
| 381 |
panic!("Tried to pop an empty stack"); |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
fn peek_crs(&self, target: &nir_block) -> Option<SyncType> { |
| 386 |
for (i, (index, sync_type)) in self.crs.iter().enumerate().rev() { |
| 387 |
if *index != target.index { |
| 388 |
continue; |
| 389 |
} |
| 390 |
|
| 391 |
match sync_type { |
| 392 |
SyncType::Sync => { |
| 393 |
// Sync must always be top-of-stack |
| 394 |
assert!(i == self.crs.len() - 1); |
| 395 |
} |
| 396 |
SyncType::Brk => { |
| 397 |
// Brk cannot skip over another Brk |
| 398 |
for (_, inner_sync) in &self.crs[(i + 1)..] { |
| 399 |
assert!(*inner_sync != SyncType::Brk); |
| 400 |
} |
| 401 |
} |
| 402 |
SyncType::Cont => { |
| 403 |
// Cont can only skip over Sync |
| 404 |
for (_, inner_sync) in &self.crs[(i + 1)..] { |
| 405 |
assert!(*inner_sync == SyncType::Sync); |
| 406 |
} |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
return Some(*sync_type); |
| 411 |
} |
| 412 |
|
| 413 |
assert!(!self.sync_blocks.contains(&target.index)); |
| 414 |
None |
| 415 |
} |
| 416 |
|
| 417 |
fn get_ssa(&mut self, ssa: &nir_def) -> &[SSAValue] { |
| 418 |
self.ssa_map.get(&ssa.index).unwrap() |
| 419 |
} |
| 420 |
|
| 421 |
fn set_ssa(&mut self, def: &nir_def, vec: Vec<SSAValue>) { |
| 422 |
if def.bit_size == 1 { |
| 423 |
for s in &vec { |
| 424 |
assert!(s.is_predicate()); |
| 425 |
} |
| 426 |
} else { |
| 427 |
for s in &vec { |
| 428 |
assert!(!s.is_predicate()); |
| 429 |
} |
| 430 |
let bits = |
| 431 |
usize::from(def.bit_size) * usize::from(def.num_components); |
| 432 |
assert!(vec.len() == bits.div_ceil(32)); |
| 433 |
} |
| 434 |
self.ssa_map |
| 435 |
.entry(def.index) |
| 436 |
.and_modify(|_| panic!("Cannot set an SSA def twice")) |
| 437 |
.or_insert(vec); |
| 438 |
} |
| 439 |
|
| 440 |
fn get_ssa_comp(&mut self, def: &nir_def, c: u8) -> (SSARef, u8) { |
| 441 |
let vec = self.get_ssa(def); |
| 442 |
match def.bit_size { |
| 443 |
1 => (vec[usize::from(c)].into(), 0), |
| 444 |
8 => (vec[usize::from(c / 4)].into(), c % 4), |
| 445 |
16 => (vec[usize::from(c / 2)].into(), (c * 2) % 4), |
| 446 |
32 => (vec[usize::from(c)].into(), 0), |
| 447 |
64 => { |
| 448 |
let comps = |
| 449 |
[vec[usize::from(c) * 2 + 0], vec[usize::from(c) * 2 + 1]]; |
| 450 |
(comps.into(), 0) |
| 451 |
} |
| 452 |
_ => panic!("Unsupported bit size: {}", def.bit_size), |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
fn get_ssa_ref(&mut self, src: &nir_src) -> SSARef { |
| 457 |
SSARef::try_from(self.get_ssa(src.as_def())).unwrap() |
| 458 |
} |
| 459 |
|
| 460 |
fn get_src(&mut self, src: &nir_src) -> Src { |
| 461 |
self.get_ssa_ref(src).into() |
| 462 |
} |
| 463 |
|
| 464 |
fn get_io_addr_offset( |
| 465 |
&mut self, |
| 466 |
addr: &nir_src, |
| 467 |
imm_bits: u8, |
| 468 |
) -> (Src, i32) { |
| 469 |
let addr = addr.as_def(); |
| 470 |
let addr_offset = unsafe { |
| 471 |
nak_get_io_addr_offset(addr as *const _ as *mut _, imm_bits) |
| 472 |
}; |
| 473 |
|
| 474 |
if let Some(base_def) = std::ptr::NonNull::new(addr_offset.base.def) { |
| 475 |
let base_def = unsafe { base_def.as_ref() }; |
| 476 |
let base_comp = u8::try_from(addr_offset.base.comp).unwrap(); |
| 477 |
let (base, _) = self.get_ssa_comp(base_def, base_comp); |
| 478 |
(base.into(), addr_offset.offset) |
| 479 |
} else { |
| 480 |
(SrcRef::Zero.into(), addr_offset.offset) |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
fn get_cbuf_addr_offset(&mut self, addr: &nir_src) -> (Src, u16) { |
| 485 |
let (off, off_imm) = self.get_io_addr_offset(addr, 16); |
| 486 |
if let Ok(off_imm_u16) = u16::try_from(off_imm) { |
| 487 |
(off, off_imm_u16) |
| 488 |
} else { |
| 489 |
(self.get_src(addr), 0) |
| 490 |
} |
| 491 |
} |
| 492 |
|
| 493 |
fn set_dst(&mut self, def: &nir_def, ssa: SSARef) { |
| 494 |
self.set_ssa(def, (*ssa).into()); |
| 495 |
} |
| 496 |
|
| 497 |
fn try_saturate_alu_dst(&mut self, def: &nir_def) -> bool { |
| 498 |
if def.all_uses_are_fsat() { |
| 499 |
self.saturated.insert(def as *const _); |
| 500 |
true |
| 501 |
} else { |
| 502 |
false |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
fn alu_src_is_saturated(&self, src: &nir_alu_src) -> bool { |
| 507 |
self.saturated.contains(&(src.src.as_def() as *const _)) |
| 508 |
} |
| 509 |
|
| 510 |
fn parse_alu(&mut self, b: &mut impl SSABuilder, alu: &nir_alu_instr) { |
| 511 |
// Handle vectors and pack ops as a special case since they're the only |
| 512 |
// ALU ops that can produce more than 16B. They are also the only ALU |
| 513 |
// ops which we allow to consume small (8 and 16-bit) vector data |
| 514 |
// scattered across multiple dwords |
| 515 |
match alu.op { |
| 516 |
nir_op_mov |
| 517 |
| nir_op_pack_32_4x8 |
| 518 |
| nir_op_pack_32_4x8_split |
| 519 |
| nir_op_pack_32_2x16 |
| 520 |
| nir_op_pack_32_2x16_split |
| 521 |
| nir_op_pack_64_2x32 |
| 522 |
| nir_op_pack_64_2x32_split |
| 523 |
| nir_op_pack_64_4x16 |
| 524 |
| nir_op_vec2 |
| 525 |
| nir_op_vec3 |
| 526 |
| nir_op_vec4 |
| 527 |
| nir_op_vec5 |
| 528 |
| nir_op_vec8 |
| 529 |
| nir_op_vec16 => { |
| 530 |
let src_bit_size = alu.get_src(0).src.bit_size(); |
| 531 |
let bits = usize::from(alu.def.num_components) |
| 532 |
* usize::from(alu.def.bit_size); |
| 533 |
|
| 534 |
// Collect the sources into a vec with src_bit_size per SSA |
| 535 |
// value in the vec. This implicitly makes 64-bit sources look |
| 536 |
// like two 32-bit values |
| 537 |
let mut srcs = Vec::new(); |
| 538 |
if alu.info().num_inputs == 1 { |
| 539 |
let src = alu.get_src(0); |
| 540 |
for c in 0..alu.src_components(0) { |
| 541 |
let s = src.swizzle[usize::from(c)]; |
| 542 |
let (src, byte) = |
| 543 |
self.get_ssa_comp(src.src.as_def(), s); |
| 544 |
for ssa in src.iter() { |
| 545 |
srcs.push((*ssa, byte)); |
| 546 |
} |
| 547 |
} |
| 548 |
} else { |
| 549 |
for src in alu.srcs_as_slice().iter() { |
| 550 |
let s = src.swizzle[0]; |
| 551 |
let (src, byte) = |
| 552 |
self.get_ssa_comp(src.src.as_def(), s); |
| 553 |
for ssa in src.iter() { |
| 554 |
srcs.push((*ssa, byte)); |
| 555 |
} |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
let mut comps = Vec::new(); |
| 560 |
match src_bit_size { |
| 561 |
1 | 32 | 64 => { |
| 562 |
for (ssa, byte) in srcs { |
| 563 |
assert!(byte == 0); |
| 564 |
// Always insert a copy regardless of whether or not |
| 565 |
// we need to permute bytes. It's possible that the |
| 566 |
// source is uniform but the destination is not and |
| 567 |
// we'll need a copy in that case. If the copy |
| 568 |
// isn't needed, copy-prop should clean it up for |
| 569 |
// us. |
| 570 |
comps.push(b.copy(ssa.into())); |
| 571 |
} |
| 572 |
} |
| 573 |
8 => { |
| 574 |
for dc in 0..bits.div_ceil(32) { |
| 575 |
let mut psrc = [None, None, None, None]; |
| 576 |
let mut psel = [0_u8; 4]; |
| 577 |
|
| 578 |
for b in 0..4 { |
| 579 |
let sc = dc * 4 + b; |
| 580 |
if sc < srcs.len() { |
| 581 |
let (ssa, byte) = srcs[sc]; |
| 582 |
// Deduplicate psrc entries |
| 583 |
for i in 0..4_u8 { |
| 584 |
let psrc_i = &mut psrc[usize::from(i)]; |
| 585 |
if psrc_i.is_none() { |
| 586 |
*psrc_i = Some(ssa.into()); |
| 587 |
} else if *psrc_i |
| 588 |
!= Some(Src::from(ssa)) |
| 589 |
{ |
| 590 |
continue; |
| 591 |
} |
| 592 |
psel[b] = i * 4 + byte; |
| 593 |
break; |
| 594 |
} |
| 595 |
} |
| 596 |
} |
| 597 |
|
| 598 |
let psrc = { |
| 599 |
let mut res = [Src::ZERO; 4]; |
| 600 |
|
| 601 |
for (idx, src) in psrc.into_iter().enumerate() { |
| 602 |
if let Some(src) = src { |
| 603 |
res[idx] = src; |
| 604 |
} |
| 605 |
} |
| 606 |
|
| 607 |
res |
| 608 |
}; |
| 609 |
|
| 610 |
comps.push(b.prmt4(psrc, psel)); |
| 611 |
} |
| 612 |
} |
| 613 |
16 => { |
| 614 |
for dc in 0..bits.div_ceil(32) { |
| 615 |
let mut psrc = [Src::ZERO; 2]; |
| 616 |
let mut psel = [0_u8; 4]; |
| 617 |
|
| 618 |
for w in 0..2 { |
| 619 |
let sc = dc * 2 + w; |
| 620 |
if sc < srcs.len() { |
| 621 |
let (ssa, byte) = srcs[sc]; |
| 622 |
psrc[w] = ssa.into(); |
| 623 |
psel[w * 2 + 0] = (w as u8 * 4) + byte; |
| 624 |
psel[w * 2 + 1] = (w as u8 * 4) + byte + 1; |
| 625 |
} |
| 626 |
} |
| 627 |
let [psrc0, psrc1] = psrc; |
| 628 |
comps.push(b.prmt(psrc0, psrc1, psel)); |
| 629 |
} |
| 630 |
} |
| 631 |
_ => panic!("Unknown bit size: {src_bit_size}"), |
| 632 |
} |
| 633 |
|
| 634 |
self.set_ssa(&alu.def, comps); |
| 635 |
return; |
| 636 |
} |
| 637 |
_ => (), |
| 638 |
} |
| 639 |
|
| 640 |
let nir_srcs = alu.srcs_as_slice(); |
| 641 |
let mut srcs_vec: Vec<Option<Src>> = Vec::new(); |
| 642 |
for (i, alu_src) in nir_srcs.iter().enumerate() { |
| 643 |
let bit_size = alu_src.src.bit_size(); |
| 644 |
let comps = alu.src_components(i.try_into().unwrap()); |
| 645 |
let ssa = self.get_ssa(alu_src.src.as_def()); |
| 646 |
|
| 647 |
match bit_size { |
| 648 |
1 => { |
| 649 |
assert!(comps == 1); |
| 650 |
let s = usize::from(alu_src.swizzle[0]); |
| 651 |
srcs_vec.push(Some(ssa[s].into())); |
| 652 |
} |
| 653 |
8 | 16 => { |
| 654 |
let num_bytes = usize::from(comps * (bit_size / 8)); |
| 655 |
assert!(num_bytes <= 4); |
| 656 |
|
| 657 |
let mut bytes = [0_u8; 4]; |
| 658 |
for c in 0..usize::from(comps) { |
| 659 |
let cs = alu_src.swizzle[c]; |
| 660 |
if bit_size == 8 { |
| 661 |
bytes[c] = cs; |
| 662 |
} else { |
| 663 |
bytes[c * 2 + 0] = cs * 2 + 0; |
| 664 |
bytes[c * 2 + 1] = cs * 2 + 1; |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
let mut prmt_srcs = [Src::ZERO; 4]; |
| 669 |
let mut prmt = [0_u8; 4]; |
| 670 |
for b in 0..num_bytes { |
| 671 |
for (ds, s) in prmt_srcs.iter_mut().enumerate() { |
| 672 |
let dw = ssa[usize::from(bytes[b] / 4)]; |
| 673 |
if s.is_zero() { |
| 674 |
*s = dw.into(); |
| 675 |
} else if *s != Src::from(dw) { |
| 676 |
continue; |
| 677 |
} |
| 678 |
prmt[b] = (ds as u8) * 4 + (bytes[b] % 4); |
| 679 |
break; |
| 680 |
} |
| 681 |
} |
| 682 |
|
| 683 |
srcs_vec.push(Some(b.prmt4(prmt_srcs, prmt).into())); |
| 684 |
} |
| 685 |
32 => { |
| 686 |
assert!(comps == 1); |
| 687 |
let s = usize::from(alu_src.swizzle[0]); |
| 688 |
srcs_vec.push(Some(ssa[s].into())); |
| 689 |
} |
| 690 |
64 => { |
| 691 |
assert!(comps == 1); |
| 692 |
let s = usize::from(alu_src.swizzle[0]); |
| 693 |
srcs_vec.push(Some([ssa[s * 2], ssa[s * 2 + 1]].into())); |
| 694 |
} |
| 695 |
_ => panic!("Invalid bit size: {bit_size}"), |
| 696 |
} |
| 697 |
} |
| 698 |
let mut srcs = |
| 699 |
|i: usize| -> Src { std::mem::take(&mut srcs_vec[i]).unwrap() }; |
| 700 |
|
| 701 |
// Restricts an F16v2 source to just x if the ALU op is single-component. This |
| 702 |
// must only be called for per-component sources (see nir_op_info::output_sizes |
| 703 |
// for more details). |
| 704 |
let restrict_f16v2_src = |mut src: Src| { |
| 705 |
if alu.def.num_components == 1 { |
| 706 |
src.src_swizzle = SrcSwizzle::Xx; |
| 707 |
} |
| 708 |
src |
| 709 |
}; |
| 710 |
|
| 711 |
let dst: SSARef = match alu.op { |
| 712 |
nir_op_b2b1 => { |
| 713 |
assert!(alu.get_src(0).bit_size() == 32); |
| 714 |
b.isetp(IntCmpType::I32, IntCmpOp::Ne, srcs(0), 0.into()) |
| 715 |
.into() |
| 716 |
} |
| 717 |
nir_op_b2b32 | nir_op_b2i8 | nir_op_b2i16 | nir_op_b2i32 => { |
| 718 |
b.sel(srcs(0).bnot(), 0.into(), 1.into()).into() |
| 719 |
} |
| 720 |
nir_op_b2i64 => { |
| 721 |
let lo = b.sel(srcs(0).bnot(), 0.into(), 1.into()); |
| 722 |
let hi = b.copy(0.into()); |
| 723 |
[lo, hi].into() |
| 724 |
} |
| 725 |
nir_op_b2f16 => { |
| 726 |
b.sel(srcs(0).bnot(), 0.into(), 0x3c00.into()).into() |
| 727 |
} |
| 728 |
nir_op_b2f32 => { |
| 729 |
b.sel(srcs(0).bnot(), 0.0_f32.into(), 1.0_f32.into()).into() |
| 730 |
} |
| 731 |
nir_op_b2f64 => { |
| 732 |
let lo = b.copy(0.into()); |
| 733 |
let hi = b.sel(srcs(0).bnot(), 0.into(), 0x3ff00000.into()); |
| 734 |
[lo, hi].into() |
| 735 |
} |
| 736 |
nir_op_bcsel => b.sel(srcs(0), srcs(1), srcs(2)).into(), |
| 737 |
nir_op_bfm => { |
| 738 |
let dst = b.alloc_ssa(RegFile::GPR); |
| 739 |
b.push_op(OpBMsk { |
| 740 |
dst: dst.into(), |
| 741 |
pos: srcs(1), |
| 742 |
width: srcs(0), |
| 743 |
wrap: true, |
| 744 |
}); |
| 745 |
dst.into() |
| 746 |
} |
| 747 |
nir_op_bit_count => { |
| 748 |
let dst = b.alloc_ssa(RegFile::GPR); |
| 749 |
b.push_op(OpPopC { |
| 750 |
dst: dst.into(), |
| 751 |
src: srcs(0), |
| 752 |
}); |
| 753 |
dst.into() |
| 754 |
} |
| 755 |
nir_op_bitfield_reverse => b.brev(srcs(0)).into(), |
| 756 |
nir_op_ibitfield_extract | nir_op_ubitfield_extract => { |
| 757 |
let dst = b.alloc_ssa(RegFile::GPR); |
| 758 |
let signed = !matches!(alu.op, nir_op_ubitfield_extract); |
| 759 |
if self.sm.sm() >= 70 { |
| 760 |
let shifted = b.shr(srcs(0), srcs(1), signed); |
| 761 |
b.push_op(OpSgxt { |
| 762 |
dst: dst.into(), |
| 763 |
a: shifted.into(), |
| 764 |
bits: srcs(2), |
| 765 |
signed, |
| 766 |
}); |
| 767 |
} else { |
| 768 |
let range = b.alloc_ssa(RegFile::GPR); |
| 769 |
b.push_op(OpPrmt { |
| 770 |
dst: range.into(), |
| 771 |
srcs: [srcs(1), srcs(2)], |
| 772 |
sel: 0x0040.into(), |
| 773 |
mode: PrmtMode::Index, |
| 774 |
}); |
| 775 |
|
| 776 |
b.push_op(OpBfe { |
| 777 |
dst: dst.into(), |
| 778 |
base: srcs(0), |
| 779 |
signed, |
| 780 |
range: range.into(), |
| 781 |
reverse: false, |
| 782 |
}); |
| 783 |
}; |
| 784 |
dst.into() |
| 785 |
} |
| 786 |
nir_op_extract_u8 | nir_op_extract_i8 | nir_op_extract_u16 |
| 787 |
| nir_op_extract_i16 => { |
| 788 |
let src1 = alu.get_src(1); |
| 789 |
let elem = src1.src.comp_as_uint(src1.swizzle[0]).unwrap(); |
| 790 |
let elem = u8::try_from(elem).unwrap(); |
| 791 |
|
| 792 |
match alu.op { |
| 793 |
nir_op_extract_u8 => { |
| 794 |
assert!(elem < 4); |
| 795 |
let byte = elem; |
| 796 |
let zero = 4; |
| 797 |
b.prmt(srcs(0), 0.into(), [byte, zero, zero, zero]) |
| 798 |
} |
| 799 |
nir_op_extract_i8 => { |
| 800 |
assert!(elem < 4); |
| 801 |
let byte = elem; |
| 802 |
let sign = byte | 0x8; |
| 803 |
b.prmt(srcs(0), 0.into(), [byte, sign, sign, sign]) |
| 804 |
} |
| 805 |
nir_op_extract_u16 => { |
| 806 |
assert!(elem < 2); |
| 807 |
let byte = elem * 2; |
| 808 |
let zero = 4; |
| 809 |
b.prmt(srcs(0), 0.into(), [byte, byte + 1, zero, zero]) |
| 810 |
} |
| 811 |
nir_op_extract_i16 => { |
| 812 |
assert!(elem < 2); |
| 813 |
let byte = elem * 2; |
| 814 |
let sign = (byte + 1) | 0x8; |
| 815 |
b.prmt(srcs(0), 0.into(), [byte, byte + 1, sign, sign]) |
| 816 |
} |
| 817 |
_ => panic!("Unknown extract op: {}", alu.op), |
| 818 |
} |
| 819 |
.into() |
| 820 |
} |
| 821 |
nir_op_f2f16 | nir_op_f2f16_rtne | nir_op_f2f16_rtz |
| 822 |
| nir_op_f2f32 | nir_op_f2f64 => { |
| 823 |
let src_bits = alu.get_src(0).src.bit_size(); |
| 824 |
let dst_bits = alu.def.bit_size(); |
| 825 |
let src_type = FloatType::from_bits(src_bits.into()); |
| 826 |
let dst_type = FloatType::from_bits(dst_bits.into()); |
| 827 |
|
| 828 |
let dst = b.alloc_ssa_vec(RegFile::GPR, dst_bits.div_ceil(32)); |
| 829 |
b.push_op(OpF2F { |
| 830 |
dst: dst.clone().into(), |
| 831 |
src: srcs(0), |
| 832 |
src_type: FloatType::from_bits(src_bits.into()), |
| 833 |
dst_type: dst_type, |
| 834 |
rnd_mode: match alu.op { |
| 835 |
nir_op_f2f16_rtne => FRndMode::NearestEven, |
| 836 |
nir_op_f2f16_rtz => FRndMode::Zero, |
| 837 |
_ => self.float_ctl[dst_type].rnd_mode, |
| 838 |
}, |
| 839 |
ftz: if src_bits < dst_bits { |
| 840 |
self.float_ctl[src_type].ftz |
| 841 |
} else { |
| 842 |
self.float_ctl[dst_type].ftz |
| 843 |
}, |
| 844 |
high: false, |
| 845 |
integer_rnd: false, |
| 846 |
}); |
| 847 |
dst |
| 848 |
} |
| 849 |
nir_op_find_lsb => { |
| 850 |
let rev = b.brev(srcs(0)); |
| 851 |
let dst = b.alloc_ssa(RegFile::GPR); |
| 852 |
b.push_op(OpFlo { |
| 853 |
dst: dst.into(), |
| 854 |
src: rev.into(), |
| 855 |
signed: false, |
| 856 |
return_shift_amount: true, |
| 857 |
}); |
| 858 |
dst.into() |
| 859 |
} |
| 860 |
nir_op_f2i8 | nir_op_f2i16 | nir_op_f2i32 | nir_op_f2i64 |
| 861 |
| nir_op_f2u8 | nir_op_f2u16 | nir_op_f2u32 | nir_op_f2u64 => { |
| 862 |
let src_bits = usize::from(alu.get_src(0).bit_size()); |
| 863 |
let dst_bits = alu.def.bit_size(); |
| 864 |
let src_type = FloatType::from_bits(src_bits); |
| 865 |
let dst = b.alloc_ssa_vec(RegFile::GPR, dst_bits.div_ceil(32)); |
| 866 |
let dst_is_signed = alu.info().output_type & 2 != 0; |
| 867 |
let dst_type = |
| 868 |
IntType::from_bits(dst_bits.into(), dst_is_signed); |
| 869 |
if b.sm() < 70 && dst_bits == 8 { |
| 870 |
// F2I doesn't support 8-bit destinations pre-Volta |
| 871 |
let tmp = b.alloc_ssa(RegFile::GPR); |
| 872 |
let tmp_type = IntType::from_bits(32, dst_is_signed); |
| 873 |
b.push_op(OpF2I { |
| 874 |
dst: tmp.into(), |
| 875 |
src: srcs(0), |
| 876 |
src_type, |
| 877 |
dst_type: tmp_type, |
| 878 |
rnd_mode: FRndMode::Zero, |
| 879 |
ftz: self.float_ctl[src_type].ftz, |
| 880 |
}); |
| 881 |
b.push_op(OpI2I { |
| 882 |
dst: dst.clone().into(), |
| 883 |
src: tmp.into(), |
| 884 |
src_type: tmp_type, |
| 885 |
dst_type, |
| 886 |
saturate: true, |
| 887 |
abs: false, |
| 888 |
neg: false, |
| 889 |
}); |
| 890 |
} else { |
| 891 |
b.push_op(OpF2I { |
| 892 |
dst: dst.clone().into(), |
| 893 |
src: srcs(0), |
| 894 |
src_type, |
| 895 |
dst_type, |
| 896 |
rnd_mode: FRndMode::Zero, |
| 897 |
ftz: self.float_ctl[src_type].ftz, |
| 898 |
}); |
| 899 |
} |
| 900 |
dst |
| 901 |
} |
| 902 |
nir_op_fabs | nir_op_fadd | nir_op_fneg => { |
| 903 |
let (x, y) = match alu.op { |
| 904 |
nir_op_fabs => (Src::ZERO.fneg(), srcs(0).fabs()), |
| 905 |
nir_op_fadd => (srcs(0), srcs(1)), |
| 906 |
nir_op_fneg => (Src::ZERO.fneg(), srcs(0).fneg()), |
| 907 |
_ => panic!("Unhandled case"), |
| 908 |
}; |
| 909 |
let ftype = FloatType::from_bits(alu.def.bit_size().into()); |
| 910 |
let dst; |
| 911 |
if alu.def.bit_size() == 64 { |
| 912 |
dst = b.alloc_ssa_vec(RegFile::GPR, 2); |
| 913 |
b.push_op(OpDAdd { |
| 914 |
dst: dst.clone().into(), |
| 915 |
srcs: [x, y], |
| 916 |
rnd_mode: self.float_ctl[ftype].rnd_mode, |
| 917 |
}); |
| 918 |
} else if alu.def.bit_size() == 32 { |
| 919 |
dst = b.alloc_ssa_vec(RegFile::GPR, 1); |
| 920 |
b.push_op(OpFAdd { |
| 921 |
dst: dst.clone().into(), |
| 922 |
srcs: [x, y], |
| 923 |
saturate: self.try_saturate_alu_dst(&alu.def), |
| 924 |
rnd_mode: self.float_ctl[ftype].rnd_mode, |
| 925 |
ftz: self.float_ctl[ftype].ftz, |
| 926 |
}); |
| 927 |
} else if alu.def.bit_size() == 16 { |
| 928 |
assert!( |
| 929 |
self.float_ctl[ftype].rnd_mode == FRndMode::NearestEven |
| 930 |
); |
| 931 |
|
| 932 |
dst = b.alloc_ssa_vec(RegFile::GPR, 1); |
| 933 |
b.push_op(OpHAdd2 { |
| 934 |
dst: dst.clone().into(), |
| 935 |
srcs: [restrict_f16v2_src(x), restrict_f16v2_src(y)], |
| 936 |
saturate: self.try_saturate_alu_dst(&alu.def), |
| 937 |
ftz: self.float_ctl[ftype].ftz, |
| 938 |
f32: false, |
| 939 |
}); |
| 940 |
} else { |
| 941 |
panic!("Unsupported float type: f{}", alu.def.bit_size()); |
| 942 |
} |
| 943 |
dst |
| 944 |
} |
| 945 |
nir_op_fceil | nir_op_ffloor | nir_op_fround_even |
| 946 |
| nir_op_ftrunc => { |
| 947 |
let dst = b.alloc_ssa(RegFile::GPR); |
| 948 |
let ty = FloatType::from_bits(alu.def.bit_size().into()); |
| 949 |
let rnd_mode = match alu.op { |
| 950 |
nir_op_fceil => FRndMode::PosInf, |
| 951 |
nir_op_ffloor => FRndMode::NegInf, |
| 952 |
nir_op_ftrunc => FRndMode::Zero, |
| 953 |
nir_op_fround_even => FRndMode::NearestEven, |
| 954 |
_ => unreachable!(), |
| 955 |
}; |
| 956 |
let ftz = self.float_ctl[ty].ftz; |
| 957 |
if b.sm() >= 70 { |
| 958 |
assert!( |
| 959 |
alu.def.bit_size() == 32 || alu.def.bit_size() == 16 |
| 960 |
); |
| 961 |
b.push_op(OpFRnd { |
| 962 |
dst: dst.into(), |
| 963 |
src: srcs(0), |
| 964 |
src_type: ty, |
| 965 |
dst_type: ty, |
| 966 |
rnd_mode, |
| 967 |
ftz, |
| 968 |
}); |
| 969 |
} else { |
| 970 |
assert!(alu.def.bit_size() == 32); |
| 971 |
b.push_op(OpF2F { |
| 972 |
dst: dst.into(), |
| 973 |
src: srcs(0), |
| 974 |
src_type: ty, |
| 975 |
dst_type: ty, |
| 976 |
rnd_mode, |
| 977 |
ftz, |
| 978 |
integer_rnd: true, |
| 979 |
high: false, |
| 980 |
}); |
| 981 |
} |
| 982 |
dst.into() |
| 983 |
} |
| 984 |
nir_op_fcos => b.fcos(srcs(0)).into(), |
| 985 |
nir_op_feq | nir_op_fge | nir_op_flt | nir_op_fneu => { |
| 986 |
let src_type = |
| 987 |
FloatType::from_bits(alu.get_src(0).bit_size().into()); |
| 988 |
let cmp_op = match alu.op { |
| 989 |
nir_op_feq => FloatCmpOp::OrdEq, |
| 990 |
nir_op_fge => FloatCmpOp::OrdGe, |
| 991 |
nir_op_flt => FloatCmpOp::OrdLt, |
| 992 |
nir_op_fneu => FloatCmpOp::UnordNe, |
| 993 |
_ => panic!("Usupported float comparison"), |
| 994 |
}; |
| 995 |
|
| 996 |
let dst = |
| 997 |
b.alloc_ssa_vec(RegFile::Pred, alu.def.num_components); |
| 998 |
if alu.get_src(0).bit_size() == 64 { |
| 999 |
assert!(alu.def.num_components == 1); |
| 1000 |
b.push_op(OpDSetP { |
| 1001 |
dst: dst.clone().into(), |
| 1002 |
set_op: PredSetOp::And, |
| 1003 |
cmp_op: cmp_op, |
| 1004 |
srcs: [srcs(0), srcs(1)], |
| 1005 |
accum: SrcRef::True.into(), |
| 1006 |
}); |
| 1007 |
} else if alu.get_src(0).bit_size() == 32 { |
| 1008 |
assert!(alu.def.num_components == 1); |
| 1009 |
b.push_op(OpFSetP { |
| 1010 |
dst: dst.clone().into(), |
| 1011 |
set_op: PredSetOp::And, |
| 1012 |
cmp_op: cmp_op, |
| 1013 |
srcs: [srcs(0), srcs(1)], |
| 1014 |
accum: SrcRef::True.into(), |
| 1015 |
ftz: self.float_ctl[src_type].ftz, |
| 1016 |
}); |
| 1017 |
} else if alu.get_src(0).bit_size() == 16 { |
| 1018 |
assert!( |
| 1019 |
alu.def.num_components == 1 |
| 1020 |
|| alu.def.num_components == 2 |
| 1021 |
); |
| 1022 |
|
| 1023 |
let dsts = if alu.def.num_components == 2 { |
| 1024 |
[dst[0].into(), dst[1].into()] |
| 1025 |
} else { |
| 1026 |
[dst[0].into(), Dst::None] |
| 1027 |
}; |
| 1028 |
|
| 1029 |
b.push_op(OpHSetP2 { |
| 1030 |
dsts, |
| 1031 |
set_op: PredSetOp::And, |
| 1032 |
cmp_op: cmp_op, |
| 1033 |
srcs: [ |
| 1034 |
restrict_f16v2_src(srcs(0)), |
| 1035 |
restrict_f16v2_src(srcs(1)), |
| 1036 |
], |
| 1037 |
accum: SrcRef::True.into(), |
| 1038 |
ftz: self.float_ctl[src_type].ftz, |
| 1039 |
horizontal: false, |
| 1040 |
}); |
| 1041 |
} else { |
| 1042 |
panic!( |
| 1043 |
"Unsupported float type: f{}", |
| 1044 |
alu.get_src(0).bit_size() |
| 1045 |
); |
| 1046 |
} |
| 1047 |
dst |
| 1048 |
} |
| 1049 |
nir_op_fexp2 => b.fexp2(srcs(0)).into(), |
| 1050 |
nir_op_ffma => { |
| 1051 |
let ftype = FloatType::from_bits(alu.def.bit_size().into()); |
| 1052 |
let dst; |
| 1053 |
if alu.def.bit_size() == 64 { |
| 1054 |
debug_assert!(!self.float_ctl[ftype].ftz); |
| 1055 |
dst = b.alloc_ssa_vec(RegFile::GPR, 2); |
| 1056 |
b.push_op(OpDFma { |
| 1057 |
dst: dst.clone().into(), |
| 1058 |
srcs: [srcs(0), srcs(1), srcs(2)], |
| 1059 |
rnd_mode: self.float_ctl[ftype].rnd_mode, |
| 1060 |
}); |
| 1061 |
} else if alu.def.bit_size() == 32 { |
| 1062 |
dst = b.alloc_ssa_vec(RegFile::GPR, 1); |
| 1063 |
b.push_op(OpFFma { |
| 1064 |
dst: dst.clone().into(), |
| 1065 |
srcs: [srcs(0), srcs(1), srcs(2)], |
| 1066 |
saturate: self.try_saturate_alu_dst(&alu.def), |
| 1067 |
rnd_mode: self.float_ctl[ftype].rnd_mode, |
| 1068 |
// The hardware doesn't like FTZ+DNZ and DNZ implies FTZ |
| 1069 |
// anyway so only set one of the two bits. |
| 1070 |
ftz: self.float_ctl[ftype].ftz, |
| 1071 |
dnz: false, |
| 1072 |
}); |
| 1073 |
} else if alu.def.bit_size() == 16 { |
| 1074 |
assert!( |
| 1075 |
self.float_ctl[ftype].rnd_mode == FRndMode::NearestEven |
| 1076 |
); |
| 1077 |
|
| 1078 |
dst = b.alloc_ssa_vec(RegFile::GPR, 1); |
| 1079 |
b.push_op(OpHFma2 { |
| 1080 |
dst: dst.clone().into(), |
| 1081 |
srcs: [ |
| 1082 |
restrict_f16v2_src(srcs(0)), |
| 1083 |
restrict_f16v2_src(srcs(1)), |
| 1084 |
restrict_f16v2_src(srcs(2)), |
| 1085 |
], |
| 1086 |
saturate: self.try_saturate_alu_dst(&alu.def), |
| 1087 |
ftz: self.float_ctl[ftype].ftz, |
| 1088 |
dnz: false, |
| 1089 |
f32: false, |
| 1090 |
}); |
| 1091 |
} else { |
| 1092 |
panic!("Unsupported float type: f{}", alu.def.bit_size()); |
| 1093 |
} |
| 1094 |
dst |
| 1095 |
} |
| 1096 |
nir_op_ffmaz => { |
| 1097 |
assert!(alu.def.bit_size() == 32); |
| 1098 |
// DNZ implies FTZ so we need FTZ set or this is invalid |
| 1099 |
assert!(self.float_ctl.fp32.ftz); |
| 1100 |
let dst = b.alloc_ssa(RegFile::GPR); |
| 1101 |
b.push_op(OpFFma { |
| 1102 |
dst: dst.into(), |
| 1103 |
srcs: [srcs(0), srcs(1), srcs(2)], |
| 1104 |
saturate: self.try_saturate_alu_dst(&alu.def), |
| 1105 |
rnd_mode: self.float_ctl.fp32.rnd_mode, |
| 1106 |
// The hardware doesn't like FTZ+DNZ and DNZ implies FTZ |
| 1107 |
// anyway so only set one of the two bits. |
| 1108 |
ftz: false, |
| 1109 |
dnz: true, |
| 1110 |
}); |
| 1111 |
dst.into() |
| 1112 |
} |
| 1113 |
nir_op_flog2 => { |
| 1114 |
assert!(alu.def.bit_size() == 32); |
| 1115 |
b.mufu(MuFuOp::Log2, srcs(0)).into() |
| 1116 |
} |
| 1117 |
nir_op_fmax | nir_op_fmin => { |
| 1118 |
let dst; |
| 1119 |
if alu.def.bit_size() == 64 { |
| 1120 |
dst = b.alloc_ssa_vec(RegFile::GPR, 2); |
| 1121 |
b.push_op(OpDMnMx { |
| 1122 |
dst: dst.clone().into(), |
| 1123 |
srcs: [srcs(0), srcs(1)], |
| 1124 |
min: (alu.op == nir_op_fmin).into(), |
| 1125 |
}); |
| 1126 |
} else if alu.def.bit_size() == 32 { |
| 1127 |
dst = b.alloc_ssa_vec(RegFile::GPR, 1); |
| 1128 |
b.push_op(OpFMnMx { |
| 1129 |
dst: dst.clone().into(), |
| 1130 |
srcs: [srcs(0), srcs(1)], |
| 1131 |
min: (alu.op == nir_op_fmin).into(), |
| 1132 |
ftz: self.float_ctl.fp32.ftz, |
| 1133 |
}); |
| 1134 |
} else if alu.def.bit_size() == 16 { |
| 1135 |
dst = b.alloc_ssa_vec(RegFile::GPR, 1); |
| 1136 |
b.push_op(OpHMnMx2 { |
| 1137 |
dst: dst.clone().into(), |
| 1138 |
srcs: [ |
| 1139 |
restrict_f16v2_src(srcs(0)), |
| 1140 |
restrict_f16v2_src(srcs(1)), |
| 1141 |
], |
| 1142 |
min: (alu.op == nir_op_fmin).into(), |
| 1143 |
ftz: self.float_ctl.fp16.ftz, |
| 1144 |
}); |
| 1145 |
} else { |
| 1146 |
panic!("Unsupported float type: f{}", alu.def.bit_size()); |
| 1147 |
} |
| 1148 |
dst |
| 1149 |
} |
| 1150 |
nir_op_fmul => { |
| 1151 |
let ftype = FloatType::from_bits(alu.def.bit_size().into()); |
| 1152 |
let dst; |
| 1153 |
if alu.def.bit_size() == 64 { |
| 1154 |
debug_assert!(!self.float_ctl[ftype].ftz); |
| 1155 |
dst = b.alloc_ssa_vec(RegFile::GPR, 2); |
| 1156 |
b.push_op(OpDMul { |
| 1157 |
dst: dst.clone().into(), |
| 1158 |
srcs: [srcs(0), srcs(1)], |
| 1159 |
rnd_mode: self.float_ctl[ftype].rnd_mode, |
| 1160 |
}); |
| 1161 |
} else if alu.def.bit_size() == 32 { |
| 1162 |
dst = b.alloc_ssa_vec(RegFile::GPR, 1); |
| 1163 |
b.push_op(OpFMul { |
| 1164 |
dst: dst.clone().into(), |
| 1165 |
srcs: [srcs(0), srcs(1)], |
| 1166 |
saturate: self.try_saturate_alu_dst(&alu.def), |
| 1167 |
rnd_mode: self.float_ctl[ftype].rnd_mode, |
| 1168 |
ftz: self.float_ctl[ftype].ftz, |
| 1169 |
dnz: false, |
| 1170 |
}); |
| 1171 |
} else if alu.def.bit_size() == 16 { |
| 1172 |
assert!( |
| 1173 |
self.float_ctl[ftype].rnd_mode == FRndMode::NearestEven |
| 1174 |
); |
| 1175 |
|
| 1176 |
dst = b.alloc_ssa_vec(RegFile::GPR, 1); |
| 1177 |
b.push_op(OpHMul2 { |
| 1178 |
dst: dst.clone().into(), |
| 1179 |
srcs: [ |
| 1180 |
restrict_f16v2_src(srcs(0)), |
| 1181 |
restrict_f16v2_src(srcs(1)), |
| 1182 |
], |
| 1183 |
saturate: self.try_saturate_alu_dst(&alu.def), |
| 1184 |
ftz: self.float_ctl[ftype].ftz, |
| 1185 |
dnz: false, |
| 1186 |
}); |
| 1187 |
} else { |
| 1188 |
panic!("Unsupported float type: f{}", alu.def.bit_size()); |
| 1189 |
} |
| 1190 |
dst |
| 1191 |
} |
| 1192 |
nir_op_fmulz => { |
| 1193 |
assert!(alu.def.bit_size() == 32); |
| 1194 |
// DNZ implies FTZ so we need FTZ set or this is invalid |
| 1195 |
assert!(self.float_ctl.fp32.ftz); |
| 1196 |
let dst = b.alloc_ssa(RegFile::GPR); |
| 1197 |
b.push_op(OpFMul { |
| 1198 |
dst: dst.into(), |
| 1199 |
srcs: [srcs(0), srcs(1)], |
| 1200 |
saturate: self.try_saturate_alu_dst(&alu.def), |
| 1201 |
rnd_mode: self.float_ctl.fp32.rnd_mode, |
| 1202 |
// The hardware doesn't like FTZ+DNZ and DNZ implies FTZ |
| 1203 |
// anyway so only set one of the two bits. |
| 1204 |
ftz: false, |
| 1205 |
dnz: true, |
| 1206 |
}); |
| 1207 |
dst.into() |
| 1208 |
} |
| 1209 |
nir_op_fquantize2f16 => { |
| 1210 |
let src0 = srcs(0); |
| 1211 |
let tmp = b.alloc_ssa(RegFile::GPR); |
| 1212 |
b.push_op(OpF2F { |
| 1213 |
dst: tmp.into(), |
| 1214 |
src: src0.clone(), |
| 1215 |
src_type: FloatType::F32, |
| 1216 |
dst_type: FloatType::F16, |
| 1217 |
rnd_mode: FRndMode::NearestEven, |
| 1218 |
ftz: true, |
| 1219 |
high: false, |
| 1220 |
integer_rnd: false, |
| 1221 |
}); |
| 1222 |
assert!(alu.def.bit_size() == 32); |
| 1223 |
let dst = b.alloc_ssa(RegFile::GPR); |
| 1224 |
b.push_op(OpF2F { |
| 1225 |
dst: dst.into(), |
| 1226 |
src: tmp.into(), |
| 1227 |
src_type: FloatType::F16, |
| 1228 |
dst_type: FloatType::F32, |
| 1229 |
rnd_mode: FRndMode::NearestEven, |
| 1230 |
ftz: true, |
| 1231 |
high: false, |
| 1232 |
integer_rnd: false, |
| 1233 |
}); |
| 1234 |
if b.sm() < 70 { |
| 1235 |
// Pre-Volta, F2F.ftz doesn't flush denorms so we need to do |
| 1236 |
// that manually |
| 1237 |
let denorm = b.fsetp( |
| 1238 |
FloatCmpOp::OrdLt, |
| 1239 |
src0.clone().fabs(), |
| 1240 |
0x38800000.into(), |
| 1241 |
); |
| 1242 |
// Get the correctly signed zero |
| 1243 |
let zero = b.lop2(LogicOp2::And, src0, 0x80000000.into()); |
| 1244 |
b.sel(denorm.into(), zero.into(), dst.into()) |
| 1245 |
} else { |
| 1246 |
dst |
| 1247 |
} |
| 1248 |
.into() |
| 1249 |
} |
| 1250 |
nir_op_frcp => { |
| 1251 |
assert!(alu.def.bit_size() == 32); |
| 1252 |
b.mufu(MuFuOp::Rcp, srcs(0)).into() |
| 1253 |
} |
| 1254 |
nir_op_frsq => { |
| 1255 |
assert!(alu.def.bit_size() == 32); |
| 1256 |
b.mufu(MuFuOp::Rsq, srcs(0)).into() |
| 1257 |
} |
| 1258 |
nir_op_fsat => { |
| 1259 |
let ftype = FloatType::from_bits(alu.def.bit_size().into()); |
| 1260 |
|
| 1261 |
if self.alu_src_is_saturated(&alu.srcs_as_slice()[0]) { |
| 1262 |
b.copy(srcs(0)).into() |
| 1263 |
} else if alu.def.bit_size() == 32 { |
| 1264 |
let dst = b.alloc_ssa(RegFile::GPR); |
| 1265 |
b.push_op(OpFAdd { |
| 1266 |
dst: dst.into(), |
| 1267 |
srcs: [srcs(0), 0.into()], |
| 1268 |
saturate: true, |
| 1269 |
rnd_mode: self.float_ctl[ftype].rnd_mode, |
| 1270 |
ftz: self.float_ctl[ftype].ftz, |
| 1271 |
}); |
| 1272 |
dst.into() |
| 1273 |
} else if alu.def.bit_size() == 16 { |
| 1274 |
assert!( |
| 1275 |
self.float_ctl[ftype].rnd_mode == FRndMode::NearestEven |
| 1276 |
); |
| 1277 |
|
| 1278 |
let dst = b.alloc_ssa(RegFile::GPR); |
| 1279 |
b.push_op(OpHAdd2 { |
| 1280 |
dst: dst.into(), |
| 1281 |
srcs: [restrict_f16v2_src(srcs(0)), 0.into()], |
| 1282 |
saturate: true, |
| 1283 |
ftz: self.float_ctl[ftype].ftz, |
| 1284 |
f32: false, |
| 1285 |
}); |
| 1286 |
dst.into() |
| 1287 |
} else { |
| 1288 |
panic!("Unsupported float type: f{}", alu.def.bit_size()); |
| 1289 |
} |
| 1290 |
} |
| 1291 |
nir_op_fsign => { |
| 1292 |
let src0 = srcs(0); |
| 1293 |
if alu.def.bit_size() == 64 { |
| 1294 |
let lz = b.dsetp(FloatCmpOp::OrdLt, src0.clone(), 0.into()); |
| 1295 |
let gz = b.dsetp(FloatCmpOp::OrdGt, src0, 0.into()); |
| 1296 |
let hi = b.sel(lz.into(), 0xbff00000.into(), 0.into()); |
| 1297 |
let hi = b.sel(gz.into(), 0x3ff00000.into(), hi.into()); |
| 1298 |
let lo = b.copy(0.into()); |
| 1299 |
[lo, hi].into() |
| 1300 |
} else if alu.def.bit_size() == 32 { |
| 1301 |
let lz = b.fset(FloatCmpOp::OrdLt, src0.clone(), 0.into()); |
| 1302 |
let gz = b.fset(FloatCmpOp::OrdGt, src0, 0.into()); |
| 1303 |
b.fadd(gz.into(), Src::from(lz).fneg()).into() |
| 1304 |
} else if alu.def.bit_size() == 16 { |
| 1305 |
let x = restrict_f16v2_src(src0); |
| 1306 |
|
| 1307 |
let lz = restrict_f16v2_src( |
| 1308 |
b.hset2(FloatCmpOp::OrdLt, x.clone(), 0.into()).into(), |
| 1309 |
); |
| 1310 |
let gz = restrict_f16v2_src( |
| 1311 |
b.hset2(FloatCmpOp::OrdGt, x, 0.into()).into(), |
| 1312 |
); |
| 1313 |
|
| 1314 |
b.hadd2(gz, lz.fneg()).into() |
| 1315 |
} else { |
| 1316 |
panic!("Unsupported float type: f{}", alu.def.bit_size()); |
| 1317 |
} |
| 1318 |
} |
| 1319 |
nir_op_fsin => b.fsin(srcs(0)).into(), |
| 1320 |
nir_op_fsqrt => b.mufu(MuFuOp::Sqrt, srcs(0)).into(), |
| 1321 |
nir_op_i2f16 | nir_op_i2f32 | nir_op_i2f64 => { |
| 1322 |
let src_bits = alu.get_src(0).src.bit_size(); |
| 1323 |
let dst_bits = alu.def.bit_size(); |
| 1324 |
let dst_type = FloatType::from_bits(dst_bits.into()); |
| 1325 |
let dst = b.alloc_ssa_vec(RegFile::GPR, dst_bits.div_ceil(32)); |
| 1326 |
b.push_op(OpI2F { |
| 1327 |
dst: dst.clone().into(), |
| 1328 |
src: srcs(0), |
| 1329 |
dst_type: dst_type, |
| 1330 |
src_type: IntType::from_bits(src_bits.into(), true), |
| 1331 |
rnd_mode: self.float_ctl[dst_type].rnd_mode, |
| 1332 |
}); |
| 1333 |
dst |
| 1334 |
} |
| 1335 |
nir_op_i2i8 | nir_op_i2i16 | nir_op_i2i32 | nir_op_i2i64 |
| 1336 |
| nir_op_u2u8 | nir_op_u2u16 | nir_op_u2u32 | nir_op_u2u64 => { |
| 1337 |
let src_bits = alu.get_src(0).src.bit_size(); |
| 1338 |
let dst_bits = alu.def.bit_size(); |
| 1339 |
|
| 1340 |
let mut prmt = [0_u8; 8]; |
| 1341 |
match alu.op { |
| 1342 |
nir_op_i2i8 | nir_op_i2i16 | nir_op_i2i32 |
| 1343 |
| nir_op_i2i64 => { |
| 1344 |
let sign = ((src_bits / 8) - 1) | 0x8; |
| 1345 |
for i in 0..8 { |
| 1346 |
if i < (src_bits / 8) { |
| 1347 |
prmt[usize::from(i)] = i; |
| 1348 |
} else { |
| 1349 |
prmt[usize::from(i)] = sign; |
| 1350 |
} |
| 1351 |
} |
| 1352 |
} |
| 1353 |
nir_op_u2u8 | nir_op_u2u16 | nir_op_u2u32 |
| 1354 |
| nir_op_u2u64 => { |
| 1355 |
for i in 0..8 { |
| 1356 |
if i < (src_bits / 8) { |
| 1357 |
prmt[usize::from(i)] = i; |
| 1358 |
} else { |
| 1359 |
prmt[usize::from(i)] = 4; |
| 1360 |
} |
| 1361 |
} |
| 1362 |
} |
| 1363 |
_ => panic!("Invalid integer conversion: {}", alu.op), |
| 1364 |
} |
| 1365 |
let prmt_lo: [u8; 4] = prmt[0..4].try_into().unwrap(); |
| 1366 |
let prmt_hi: [u8; 4] = prmt[4..8].try_into().unwrap(); |
| 1367 |
|
| 1368 |
let src = srcs(0).to_ssa(); |
| 1369 |
if src_bits == 64 { |
| 1370 |
if dst_bits == 64 { |
| 1371 |
src |
| 1372 |
} else { |
| 1373 |
b.prmt(src[0].into(), src[1].into(), prmt_lo).into() |
| 1374 |
} |
| 1375 |
} else { |
| 1376 |
if dst_bits == 64 { |
| 1377 |
let lo = b.prmt(src[0].into(), 0.into(), prmt_lo); |
| 1378 |
let hi = b.prmt(src[0].into(), 0.into(), prmt_hi); |
| 1379 |
[lo, hi].into() |
| 1380 |
} else { |
| 1381 |
b.prmt(src[0].into(), 0.into(), prmt_lo).into() |
| 1382 |
} |
| 1383 |
} |
| 1384 |
} |
| 1385 |
nir_op_iabs => b.iabs(srcs(0)).into(), |
| 1386 |
nir_op_iadd => match alu.def.bit_size { |
| 1387 |
32 => b.iadd(srcs(0), srcs(1), 0.into()).into(), |
| 1388 |
64 => b.iadd64(srcs(0), srcs(1), 0.into()), |
| 1389 |
x => panic!("unsupported bit size for nir_op_iadd: {x}"), |
| 1390 |
}, |
| 1391 |
nir_op_iadd3 => match alu.def.bit_size { |
| 1392 |
32 => b.iadd(srcs(0), srcs(1), srcs(2)).into(), |
| 1393 |
64 => b.iadd64(srcs(0), srcs(1), srcs(2)), |
| 1394 |
x => panic!("unsupported bit size for nir_op_iadd3: {x}"), |
| 1395 |
}, |
| 1396 |
nir_op_iand => b.lop2(LogicOp2::And, srcs(0), srcs(1)).into(), |
| 1397 |
nir_op_ieq => { |
| 1398 |
if alu.get_src(0).bit_size() == 1 { |
| 1399 |
b.lop2(LogicOp2::Xor, srcs(0), srcs(1).bnot()).into() |
| 1400 |
} else if alu.get_src(0).bit_size() == 64 { |
| 1401 |
b.isetp64(IntCmpType::I32, IntCmpOp::Eq, srcs(0), srcs(1)) |
| 1402 |
} else { |
| 1403 |
assert!(alu.get_src(0).bit_size() == 32); |
| 1404 |
b.isetp(IntCmpType::I32, IntCmpOp::Eq, srcs(0), srcs(1)) |
| 1405 |
.into() |
| 1406 |
} |
| 1407 |
} |
| 1408 |
nir_op_ifind_msb | nir_op_ifind_msb_rev | nir_op_ufind_msb |
| 1409 |
| nir_op_ufind_msb_rev => { |
| 1410 |
let dst = b.alloc_ssa(RegFile::GPR); |
| 1411 |
b.push_op(OpFlo { |
| 1412 |
dst: dst.into(), |
| 1413 |
src: srcs(0), |
| 1414 |
signed: match alu.op { |
| 1415 |
nir_op_ifind_msb | nir_op_ifind_msb_rev => true, |
| 1416 |
nir_op_ufind_msb | nir_op_ufind_msb_rev => false, |
| 1417 |
_ => panic!("Not a find_msb op"), |
| 1418 |
}, |
| 1419 |
return_shift_amount: match alu.op { |
| 1420 |
nir_op_ifind_msb | nir_op_ufind_msb => false, |
| 1421 |
nir_op_ifind_msb_rev | nir_op_ufind_msb_rev => true, |
| 1422 |
_ => panic!("Not a find_msb op"), |
| 1423 |
}, |
| 1424 |
}); |
| 1425 |
dst.into() |
| 1426 |
} |
| 1427 |
nir_op_ige | nir_op_ilt | nir_op_uge | nir_op_ult => { |
| 1428 |
let x = srcs(0).to_ssa(); |
| 1429 |
let y = srcs(1).to_ssa(); |
| 1430 |
let (cmp_type, cmp_op) = match alu.op { |
| 1431 |
nir_op_ige => (IntCmpType::I32, IntCmpOp::Ge), |
| 1432 |
nir_op_ilt => (IntCmpType::I32, IntCmpOp::Lt), |
| 1433 |
nir_op_uge => (IntCmpType::U32, IntCmpOp::Ge), |
| 1434 |
nir_op_ult => (IntCmpType::U32, IntCmpOp::Lt), |
| 1435 |
_ => panic!("Not an integer comparison"), |
| 1436 |
}; |
| 1437 |
if alu.get_src(0).bit_size() == 64 { |
| 1438 |
b.isetp64(cmp_type, cmp_op, x.into(), y.into()) |
| 1439 |
} else { |
| 1440 |
assert!(alu.get_src(0).bit_size() == 32); |
| 1441 |
b.isetp(cmp_type, cmp_op, x.into(), y.into()).into() |
| 1442 |
} |
| 1443 |
} |
| 1444 |
nir_op_imad => { |
| 1445 |
assert!(alu.def.bit_size() == 32); |
| 1446 |
let dst = b.alloc_ssa(RegFile::GPR); |
| 1447 |
b.push_op(OpIMad { |
| 1448 |
dst: dst.into(), |
| 1449 |
srcs: [srcs(0), srcs(1), srcs(2)], |
| 1450 |
signed: false, |
| 1451 |
}); |
| 1452 |
dst.into() |
| 1453 |
} |
| 1454 |
nir_op_imax | nir_op_imin | nir_op_umax | nir_op_umin => { |
| 1455 |
let (tp, min) = match alu.op { |
| 1456 |
nir_op_imax => (IntCmpType::I32, SrcRef::False), |
| 1457 |
nir_op_imin => (IntCmpType::I32, SrcRef::True), |
| 1458 |
nir_op_umax => (IntCmpType::U32, SrcRef::False), |
| 1459 |
nir_op_umin => (IntCmpType::U32, SrcRef::True), |
| 1460 |
_ => panic!("Not an integer min/max"), |
| 1461 |
}; |
| 1462 |
assert!(alu.def.bit_size() == 32); |
| 1463 |
b.imnmx(tp, srcs(0), srcs(1), min.into()).into() |
| 1464 |
} |
| 1465 |
nir_op_imul => { |
| 1466 |
assert!(alu.def.bit_size() == 32); |
| 1467 |
b.imul(srcs(0), srcs(1)).into() |
| 1468 |
} |
| 1469 |
nir_op_imul_2x32_64 | nir_op_umul_2x32_64 => { |
| 1470 |
let signed = alu.op == nir_op_imul_2x32_64; |
| 1471 |
b.imul_2x32_64(srcs(0), srcs(1), signed) |
| 1472 |
} |
| 1473 |
nir_op_imul_high | nir_op_umul_high => { |
| 1474 |
let signed = alu.op == nir_op_imul_high; |
| 1475 |
let dst64 = b.imul_2x32_64(srcs(0), srcs(1), signed); |
| 1476 |
dst64[1].into() |
| 1477 |
} |
| 1478 |
nir_op_ine => { |
| 1479 |
if alu.get_src(0).bit_size() == 1 { |
| 1480 |
b.lop2(LogicOp2::Xor, srcs(0), srcs(1)).into() |
| 1481 |
} else if alu.get_src(0).bit_size() == 64 { |
| 1482 |
b.isetp64(IntCmpType::I32, IntCmpOp::Ne, srcs(0), srcs(1)) |
| 1483 |
} else { |
| 1484 |
assert!(alu.get_src(0).bit_size() == 32); |
| 1485 |
b.isetp(IntCmpType::I32, IntCmpOp::Ne, srcs(0), srcs(1)) |
| 1486 |
.into() |
| 1487 |
} |
| 1488 |
} |
| 1489 |
nir_op_ineg => { |
| 1490 |
if alu.def.bit_size == 64 { |
| 1491 |
b.ineg64(srcs(0)) |
| 1492 |
} else { |
| 1493 |
assert!(alu.def.bit_size() == 32); |
| 1494 |
b.ineg(srcs(0)).into() |
| 1495 |
} |
| 1496 |
} |
| 1497 |
nir_op_inot => { |
| 1498 |
if alu.def.bit_size() == 1 { |
| 1499 |
b.lop2(LogicOp2::PassB, true.into(), srcs(0).bnot()).into() |
| 1500 |
} else { |
| 1501 |
assert!(alu.def.bit_size() == 32); |
| 1502 |
b.lop2(LogicOp2::PassB, 0.into(), srcs(0).bnot()).into() |
| 1503 |
} |
| 1504 |
} |
| 1505 |
nir_op_ior => b.lop2(LogicOp2::Or, srcs(0), srcs(1)).into(), |
| 1506 |
nir_op_ishl => { |
| 1507 |
if alu.def.bit_size() == 64 { |
| 1508 |
let shift = if let Some(s) = nir_srcs[1].comp_as_uint(0) { |
| 1509 |
(s as u32).into() |
| 1510 |
} else { |
| 1511 |
srcs(1) |
| 1512 |
}; |
| 1513 |
b.shl64(srcs(0), shift) |
| 1514 |
} else { |
| 1515 |
assert!(alu.def.bit_size() == 32); |
| 1516 |
b.shl(srcs(0), srcs(1)).into() |
| 1517 |
} |
| 1518 |
} |
| 1519 |
nir_op_ishr => { |
| 1520 |
if alu.def.bit_size() == 64 { |
| 1521 |
let shift = if let Some(s) = nir_srcs[1].comp_as_uint(0) { |
| 1522 |
(s as u32).into() |
| 1523 |
} else { |
| 1524 |
srcs(1) |
| 1525 |
}; |
| 1526 |
b.shr64(srcs(0), shift, true) |
| 1527 |
} else { |
| 1528 |
assert!(alu.def.bit_size() == 32); |
| 1529 |
b.shr(srcs(0), srcs(1), true).into() |
| 1530 |
} |
| 1531 |
} |
| 1532 |
nir_op_lea_nv => { |
| 1533 |
let src_a = srcs(1); |
| 1534 |
let src_b = srcs(0); |
| 1535 |
let shift = nir_srcs[2].comp_as_uint(0).unwrap() as u8; |
| 1536 |
match alu.def.bit_size { |
| 1537 |
32 => b.lea(src_a, src_b, shift).into(), |
| 1538 |
64 => b.lea64(src_a, src_b, shift), |
| 1539 |
x => panic!("unsupported bit size for nir_op_lea_nv: {x}"), |
| 1540 |
} |
| 1541 |
} |
| 1542 |
nir_op_isub => match alu.def.bit_size { |
| 1543 |
32 => b.iadd(srcs(0), srcs(1).ineg(), 0.into()).into(), |
| 1544 |
64 => b.iadd64(srcs(0), srcs(1).ineg(), 0.into()), |
| 1545 |
x => panic!("unsupported bit size for nir_op_iadd: {x}"), |
| 1546 |
}, |
| 1547 |
nir_op_ixor => b.lop2(LogicOp2::Xor, srcs(0), srcs(1)).into(), |
| 1548 |
nir_op_pack_half_2x16_split | nir_op_pack_half_2x16_rtz_split => { |
| 1549 |
assert!(alu.get_src(0).bit_size() == 32); |
| 1550 |
|
| 1551 |
let rnd_mode = match alu.op { |
| 1552 |
nir_op_pack_half_2x16_split => FRndMode::NearestEven, |
| 1553 |
nir_op_pack_half_2x16_rtz_split => FRndMode::Zero, |
| 1554 |
_ => panic!("Unhandled fp16 pack op"), |
| 1555 |
}; |
| 1556 |
|
| 1557 |
if self.sm.sm() >= 86 { |
| 1558 |
let result = b.alloc_ssa(RegFile::GPR); |
| 1559 |
b.push_op(OpF2FP { |
| 1560 |
dst: result.into(), |
| 1561 |
srcs: [srcs(1), srcs(0)], |
| 1562 |
rnd_mode: rnd_mode, |
| 1563 |
}); |
| 1564 |
|
| 1565 |
result.into() |
| 1566 |
} else { |
| 1567 |
let low = b.alloc_ssa(RegFile::GPR); |
| 1568 |
let high = b.alloc_ssa(RegFile::GPR); |
| 1569 |
|
| 1570 |
b.push_op(OpF2F { |
| 1571 |
dst: low.into(), |
| 1572 |
src: srcs(0), |
| 1573 |
src_type: FloatType::F32, |
| 1574 |
dst_type: FloatType::F16, |
| 1575 |
rnd_mode: rnd_mode, |
| 1576 |
ftz: false, |
| 1577 |
high: false, |
| 1578 |
integer_rnd: false, |
| 1579 |
}); |
| 1580 |
|
| 1581 |
let src_bits = usize::from(alu.get_src(1).bit_size()); |
| 1582 |
let src_type = FloatType::from_bits(src_bits); |
| 1583 |
assert!(matches!(src_type, FloatType::F32)); |
| 1584 |
b.push_op(OpF2F { |
| 1585 |
dst: high.into(), |
| 1586 |
src: srcs(1), |
| 1587 |
src_type: FloatType::F32, |
| 1588 |
dst_type: FloatType::F16, |
| 1589 |
rnd_mode: rnd_mode, |
| 1590 |
ftz: false, |
| 1591 |
high: false, |
| 1592 |
integer_rnd: false, |
| 1593 |
}); |
| 1594 |
|
| 1595 |
b.prmt(low.into(), high.into(), [0, 1, 4, 5]).into() |
| 1596 |
} |
| 1597 |
} |
| 1598 |
nir_op_prmt_nv => { |
| 1599 |
let dst = b.alloc_ssa(RegFile::GPR); |
| 1600 |
b.push_op(OpPrmt { |
| 1601 |
dst: dst.into(), |
| 1602 |
srcs: [srcs(1), srcs(2)], |
| 1603 |
sel: srcs(0), |
| 1604 |
mode: PrmtMode::Index, |
| 1605 |
}); |
| 1606 |
dst.into() |
| 1607 |
} |
| 1608 |
nir_op_sdot_4x8_iadd => { |
| 1609 |
let dst = b.alloc_ssa(RegFile::GPR); |
| 1610 |
b.push_op(OpIDp4 { |
| 1611 |
dst: dst.into(), |
| 1612 |
src_types: [IntType::I8, IntType::I8], |
| 1613 |
srcs: [srcs(0), srcs(1), srcs(2)], |
| 1614 |
}); |
| 1615 |
dst.into() |
| 1616 |
} |
| 1617 |
nir_op_sudot_4x8_iadd => { |
| 1618 |
let dst = b.alloc_ssa(RegFile::GPR); |
| 1619 |
b.push_op(OpIDp4 { |
| 1620 |
dst: dst.into(), |
| 1621 |
src_types: [IntType::I8, IntType::U8], |
| 1622 |
srcs: [srcs(0), srcs(1), srcs(2)], |
| 1623 |
}); |
| 1624 |
dst.into() |
| 1625 |
} |
| 1626 |
nir_op_udot_4x8_uadd => { |
| 1627 |
let dst = b.alloc_ssa(RegFile::GPR); |
| 1628 |
b.push_op(OpIDp4 { |
| 1629 |
dst: dst.into(), |
| 1630 |
src_types: [IntType::U8, IntType::U8], |
| 1631 |
srcs: [srcs(0), srcs(1), srcs(2)], |
| 1632 |
}); |
| 1633 |
dst.into() |
| 1634 |
} |
| 1635 |
nir_op_u2f16 | nir_op_u2f32 | nir_op_u2f64 => { |
| 1636 |
let src_bits = alu.get_src(0).src.bit_size(); |
| 1637 |
let dst_bits = alu.def.bit_size(); |
| 1638 |
let dst_type = FloatType::from_bits(dst_bits.into()); |
| 1639 |
let dst = b.alloc_ssa_vec(RegFile::GPR, dst_bits.div_ceil(32)); |
| 1640 |
b.push_op(OpI2F { |
| 1641 |
dst: dst.clone().into(), |
| 1642 |
src: srcs(0), |
| 1643 |
dst_type: dst_type, |
| 1644 |
src_type: IntType::from_bits(src_bits.into(), false), |
| 1645 |
rnd_mode: self.float_ctl[dst_type].rnd_mode, |
| 1646 |
}); |
| 1647 |
dst |
| 1648 |
} |
| 1649 |
nir_op_uadd_sat => { |
| 1650 |
let x = srcs(0).to_ssa(); |
| 1651 |
let y = srcs(1).to_ssa(); |
| 1652 |
let sum_lo = b.alloc_ssa(RegFile::GPR); |
| 1653 |
let ovf_lo = b.alloc_ssa(RegFile::Pred); |
| 1654 |
b.push_op(OpIAdd3 { |
| 1655 |
dst: sum_lo.into(), |
| 1656 |
overflow: [ovf_lo.into(), Dst::None], |
| 1657 |
srcs: [0.into(), x[0].into(), y[0].into()], |
| 1658 |
}); |
| 1659 |
if alu.def.bit_size() == 64 { |
| 1660 |
let sum_hi = b.alloc_ssa(RegFile::GPR); |
| 1661 |
let ovf_hi = b.alloc_ssa(RegFile::Pred); |
| 1662 |
b.push_op(OpIAdd3X { |
| 1663 |
dst: sum_hi.into(), |
| 1664 |
overflow: [ovf_hi.into(), Dst::None], |
| 1665 |
srcs: [0.into(), x[1].into(), y[1].into()], |
| 1666 |
carry: [ovf_lo.into(), false.into()], |
| 1667 |
}); |
| 1668 |
let lo = |
| 1669 |
b.sel(ovf_hi.into(), u32::MAX.into(), sum_lo.into()); |
| 1670 |
let hi = |
| 1671 |
b.sel(ovf_hi.into(), u32::MAX.into(), sum_hi.into()); |
| 1672 |
[lo, hi].into() |
| 1673 |
} else { |
| 1674 |
assert!(alu.def.bit_size() == 32); |
| 1675 |
b.sel(ovf_lo.into(), u32::MAX.into(), sum_lo.into()).into() |
| 1676 |
} |
| 1677 |
} |
| 1678 |
nir_op_usub_sat => { |
| 1679 |
let x = srcs(0).to_ssa(); |
| 1680 |
let y = srcs(1).to_ssa(); |
| 1681 |
let sum_lo = b.alloc_ssa(RegFile::GPR); |
| 1682 |
let ovf_lo = b.alloc_ssa(RegFile::Pred); |
| 1683 |
// The result of OpIAdd3X is the 33-bit value |
| 1684 |
// |
| 1685 |
// s|o = x + !y + 1 |
| 1686 |
// |
| 1687 |
// The overflow bit of this result is true if and only if the |
| 1688 |
// subtract did NOT overflow. |
| 1689 |
b.push_op(OpIAdd3 { |
| 1690 |
dst: sum_lo.into(), |
| 1691 |
overflow: [ovf_lo.into(), Dst::None], |
| 1692 |
srcs: [0.into(), x[0].into(), Src::from(y[0]).ineg()], |
| 1693 |
}); |
| 1694 |
if alu.def.bit_size() == 64 { |
| 1695 |
let sum_hi = b.alloc_ssa(RegFile::GPR); |
| 1696 |
let ovf_hi = b.alloc_ssa(RegFile::Pred); |
| 1697 |
b.push_op(OpIAdd3X { |
| 1698 |
dst: sum_hi.into(), |
| 1699 |
overflow: [ovf_hi.into(), Dst::None], |
| 1700 |
srcs: [0.into(), x[1].into(), Src::from(y[1]).bnot()], |
| 1701 |
carry: [ovf_lo.into(), false.into()], |
| 1702 |
}); |
| 1703 |
let lo = b.sel(ovf_hi.into(), sum_lo.into(), 0.into()); |
| 1704 |
let hi = b.sel(ovf_hi.into(), sum_hi.into(), 0.into()); |
| 1705 |
[lo, hi].into() |
| 1706 |
} else { |
| 1707 |
assert!(alu.def.bit_size() == 32); |
| 1708 |
b.sel(ovf_lo.into(), sum_lo.into(), 0.into()).into() |
| 1709 |
} |
| 1710 |
} |
| 1711 |
nir_op_unpack_32_2x16 | nir_op_unpack_32_4x8 => { |
| 1712 |
b.copy(srcs(0)).into() |
| 1713 |
} |
| 1714 |
nir_op_unpack_32_2x16_split_x => { |
| 1715 |
b.prmt(srcs(0), 0.into(), [0, 1, 4, 4]).into() |
| 1716 |
} |
| 1717 |
nir_op_unpack_32_2x16_split_y => { |
| 1718 |
b.prmt(srcs(0), 0.into(), [2, 3, 4, 4]).into() |
| 1719 |
} |
| 1720 |
nir_op_unpack_64_2x32 | nir_op_unpack_64_4x16 => { |
| 1721 |
let src0 = srcs(0).to_ssa(); |
| 1722 |
[b.copy(src0[0].into()), b.copy(src0[1].into())].into() |
| 1723 |
} |
| 1724 |
nir_op_unpack_64_2x32_split_x => { |
| 1725 |
let src0_x = srcs(0).as_ssa().unwrap()[0]; |
| 1726 |
b.copy(src0_x.into()).into() |
| 1727 |
} |
| 1728 |
nir_op_unpack_64_2x32_split_y => { |
| 1729 |
let src0_y = srcs(0).as_ssa().unwrap()[1]; |
| 1730 |
b.copy(src0_y.into()).into() |
| 1731 |
} |
| 1732 |
nir_op_unpack_half_2x16_split_x |
| 1733 |
| nir_op_unpack_half_2x16_split_y => { |
| 1734 |
assert!(alu.def.bit_size() == 32); |
| 1735 |
let dst = b.alloc_ssa(RegFile::GPR); |
| 1736 |
|
| 1737 |
b.push_op(OpF2F { |
| 1738 |
dst: dst.into(), |
| 1739 |
src: srcs(0), |
| 1740 |
src_type: FloatType::F16, |
| 1741 |
dst_type: FloatType::F32, |
| 1742 |
rnd_mode: FRndMode::NearestEven, |
| 1743 |
ftz: false, |
| 1744 |
high: alu.op == nir_op_unpack_half_2x16_split_y, |
| 1745 |
integer_rnd: false, |
| 1746 |
}); |
| 1747 |
|
| 1748 |
dst.into() |
| 1749 |
} |
| 1750 |
nir_op_ushr => { |
| 1751 |
if alu.def.bit_size() == 64 { |
| 1752 |
let shift = if let Some(s) = nir_srcs[1].comp_as_uint(0) { |
| 1753 |
(s as u32).into() |
| 1754 |
} else { |
| 1755 |
srcs(1) |
| 1756 |
}; |
| 1757 |
b.shr64(srcs(0), shift, false) |
| 1758 |
} else { |
| 1759 |
assert!(alu.def.bit_size() == 32); |
| 1760 |
b.shr(srcs(0), srcs(1), false).into() |
| 1761 |
} |
| 1762 |
} |
| 1763 |
_ => panic!("Unsupported ALU instruction: {}", alu.info().name()), |
| 1764 |
}; |
| 1765 |
self.set_dst(&alu.def, dst); |
| 1766 |
} |
| 1767 |
|
| 1768 |
fn parse_tex(&mut self, b: &mut impl SSABuilder, tex: &nir_tex_instr) { |
| 1769 |
let dim = match tex.sampler_dim { |
| 1770 |
GLSL_SAMPLER_DIM_1D => { |
| 1771 |
if tex.is_array { |
| 1772 |
TexDim::Array1D |
| 1773 |
} else { |
| 1774 |
TexDim::_1D |
| 1775 |
} |
| 1776 |
} |
| 1777 |
GLSL_SAMPLER_DIM_2D => { |
| 1778 |
if tex.is_array { |
| 1779 |
TexDim::Array2D |
| 1780 |
} else { |
| 1781 |
TexDim::_2D |
| 1782 |
} |
| 1783 |
} |
| 1784 |
GLSL_SAMPLER_DIM_3D => { |
| 1785 |
assert!(!tex.is_array); |
| 1786 |
TexDim::_3D |
| 1787 |
} |
| 1788 |
GLSL_SAMPLER_DIM_CUBE => { |
| 1789 |
if tex.is_array { |
| 1790 |
TexDim::ArrayCube |
| 1791 |
} else { |
| 1792 |
TexDim::Cube |
| 1793 |
} |
| 1794 |
} |
| 1795 |
GLSL_SAMPLER_DIM_BUF => TexDim::_1D, |
| 1796 |
GLSL_SAMPLER_DIM_MS => { |
| 1797 |
if tex.is_array { |
| 1798 |
TexDim::Array2D |
| 1799 |
} else { |
| 1800 |
TexDim::_2D |
| 1801 |
} |
| 1802 |
} |
| 1803 |
_ => panic!("Unsupported texture dimension: {}", tex.sampler_dim), |
| 1804 |
}; |
| 1805 |
|
| 1806 |
let srcs = tex.srcs_as_slice(); |
| 1807 |
assert!(srcs[0].src_type == nir_tex_src_backend1); |
| 1808 |
if srcs.len() > 1 { |
| 1809 |
assert!(srcs.len() == 2); |
| 1810 |
assert!(srcs[1].src_type == nir_tex_src_backend2); |
| 1811 |
} |
| 1812 |
|
| 1813 |
let flags: nak_nir_tex_flags = |
| 1814 |
unsafe { std::mem::transmute_copy(&tex.backend_flags) }; |
| 1815 |
|
| 1816 |
let tex_ref = match flags.ref_type() { |
| 1817 |
NAK_NIR_TEX_REF_TYPE_BOUND => { |
| 1818 |
TexRef::Bound(tex.texture_index.try_into().unwrap()) |
| 1819 |
} |
| 1820 |
NAK_NIR_TEX_REF_TYPE_CBUF => TexRef::CBuf(TexCBufRef { |
| 1821 |
idx: (tex.texture_index >> 16).try_into().unwrap(), |
| 1822 |
offset: tex.texture_index as u16, |
| 1823 |
}), |
| 1824 |
NAK_NIR_TEX_REF_TYPE_BINDLESS => TexRef::Bindless, |
| 1825 |
_ => panic!("Invalid tex ref type"), |
| 1826 |
}; |
| 1827 |
|
| 1828 |
let mask = tex.def.components_read(); |
| 1829 |
let mut mask = u8::try_from(mask).unwrap(); |
| 1830 |
if flags.is_sparse() { |
| 1831 |
mask &= !(1 << (tex.def.num_components - 1)); |
| 1832 |
if mask == 0 { |
| 1833 |
// This can happen if only the sparse predicate is used. In |
| 1834 |
// that case, we need at least one result register. |
| 1835 |
mask = 1; |
| 1836 |
} |
| 1837 |
} else { |
| 1838 |
debug_assert!(mask != 0); |
| 1839 |
} |
| 1840 |
let channel_mask = ChannelMask::new(mask); |
| 1841 |
|
| 1842 |
let dst_comps = u8::try_from(mask.count_ones()).unwrap(); |
| 1843 |
let dst = b.alloc_ssa_vec(RegFile::GPR, dst_comps); |
| 1844 |
|
| 1845 |
// On Volta and later, the destination is split in two |
| 1846 |
let mut dsts = [Dst::None, Dst::None]; |
| 1847 |
if dst_comps > 2 && b.sm() >= 70 { |
| 1848 |
dsts[0] = SSARef::try_from(&dst[0..2]).unwrap().into(); |
| 1849 |
dsts[1] = SSARef::try_from(&dst[2..]).unwrap().into(); |
| 1850 |
} else { |
| 1851 |
dsts[0] = dst.clone().into(); |
| 1852 |
} |
| 1853 |
|
| 1854 |
let fault = if flags.is_sparse() { |
| 1855 |
Some(b.alloc_ssa(RegFile::Pred)) |
| 1856 |
} else { |
| 1857 |
None |
| 1858 |
}; |
| 1859 |
|
| 1860 |
if tex.op == nir_texop_hdr_dim_nv { |
| 1861 |
let src = self.get_src(&srcs[0].src); |
| 1862 |
assert!(fault.is_none()); |
| 1863 |
b.push_op(OpTxq { |
| 1864 |
dsts: dsts, |
| 1865 |
tex: tex_ref, |
| 1866 |
src: src, |
| 1867 |
query: TexQuery::Dimension, |
| 1868 |
nodep: flags.nodep(), |
| 1869 |
channel_mask, |
| 1870 |
}); |
| 1871 |
} else if tex.op == nir_texop_tex_type_nv { |
| 1872 |
let src = self.get_src(&srcs[0].src); |
| 1873 |
assert!(fault.is_none()); |
| 1874 |
b.push_op(OpTxq { |
| 1875 |
dsts: dsts, |
| 1876 |
tex: tex_ref, |
| 1877 |
src: src, |
| 1878 |
query: TexQuery::TextureType, |
| 1879 |
nodep: flags.nodep(), |
| 1880 |
channel_mask, |
| 1881 |
}); |
| 1882 |
} else if tex.op == nir_texop_sample_pos_nv { |
| 1883 |
let src = self.get_src(&srcs[0].src); |
| 1884 |
assert!(fault.is_none()); |
| 1885 |
b.push_op(OpTxq { |
| 1886 |
dsts: dsts, |
| 1887 |
tex: tex_ref, |
| 1888 |
src: src, |
| 1889 |
query: TexQuery::SamplerPos, |
| 1890 |
nodep: flags.nodep(), |
| 1891 |
channel_mask, |
| 1892 |
}); |
| 1893 |
} else { |
| 1894 |
let lod_mode = match flags.lod_mode() { |
| 1895 |
NAK_NIR_LOD_MODE_AUTO => TexLodMode::Auto, |
| 1896 |
NAK_NIR_LOD_MODE_ZERO => TexLodMode::Zero, |
| 1897 |
NAK_NIR_LOD_MODE_BIAS => TexLodMode::Bias, |
| 1898 |
NAK_NIR_LOD_MODE_LOD => TexLodMode::Lod, |
| 1899 |
NAK_NIR_LOD_MODE_CLAMP => TexLodMode::Clamp, |
| 1900 |
NAK_NIR_LOD_MODE_BIAS_CLAMP => TexLodMode::BiasClamp, |
| 1901 |
_ => panic!("Invalid LOD mode"), |
| 1902 |
}; |
| 1903 |
|
| 1904 |
// Starting with Blackwell B, the shader stage check for derivatives |
| 1905 |
// is back to defaulting to disabled on compute and instead we have |
| 1906 |
// a new derivative mode to re-enable it. If tex_lod_mode == Zero, |
| 1907 |
// there is no implicit derivative so this doesn't matter. |
| 1908 |
let deriv_mode = |
| 1909 |
if self.sm.sm() >= 120 && lod_mode != TexLodMode::Zero { |
| 1910 |
TexDerivMode::DerivXY |
| 1911 |
} else { |
| 1912 |
TexDerivMode::Auto |
| 1913 |
}; |
| 1914 |
|
| 1915 |
let offset_mode = match flags.offset_mode() { |
| 1916 |
NAK_NIR_OFFSET_MODE_NONE => TexOffsetMode::None, |
| 1917 |
NAK_NIR_OFFSET_MODE_AOFFI => TexOffsetMode::AddOffI, |
| 1918 |
NAK_NIR_OFFSET_MODE_PER_PX => TexOffsetMode::PerPx, |
| 1919 |
_ => panic!("Invalid offset mode"), |
| 1920 |
}; |
| 1921 |
|
| 1922 |
let src0 = self.get_src(&srcs[0].src); |
| 1923 |
let src1 = if srcs.len() > 1 { |
| 1924 |
self.get_src(&srcs[1].src) |
| 1925 |
} else { |
| 1926 |
SrcRef::Zero.into() |
| 1927 |
}; |
| 1928 |
let srcs = [src0, src1]; |
| 1929 |
|
| 1930 |
if tex.op == nir_texop_txd { |
| 1931 |
assert!(lod_mode == TexLodMode::Auto); |
| 1932 |
assert!(offset_mode != TexOffsetMode::PerPx); |
| 1933 |
assert!(!flags.has_z_cmpr()); |
| 1934 |
b.push_op(OpTxd { |
| 1935 |
dsts: dsts, |
| 1936 |
fault: fault.into(), |
| 1937 |
tex: tex_ref, |
| 1938 |
srcs: srcs, |
| 1939 |
dim: dim, |
| 1940 |
offset_mode, |
| 1941 |
mem_eviction_priority: MemEvictionPriority::Normal, |
| 1942 |
nodep: flags.nodep(), |
| 1943 |
channel_mask, |
| 1944 |
}); |
| 1945 |
} else if tex.op == nir_texop_lod { |
| 1946 |
assert!(lod_mode == TexLodMode::Auto); |
| 1947 |
assert!(offset_mode == TexOffsetMode::None); |
| 1948 |
b.push_op(OpTmml { |
| 1949 |
dsts: dsts, |
| 1950 |
tex: tex_ref, |
| 1951 |
srcs: srcs, |
| 1952 |
dim: dim, |
| 1953 |
deriv_mode, |
| 1954 |
nodep: flags.nodep(), |
| 1955 |
channel_mask, |
| 1956 |
}); |
| 1957 |
} else if tex.op == nir_texop_txf || tex.op == nir_texop_txf_ms { |
| 1958 |
assert!(offset_mode != TexOffsetMode::PerPx); |
| 1959 |
b.push_op(OpTld { |
| 1960 |
dsts: dsts, |
| 1961 |
fault: fault.into(), |
| 1962 |
tex: tex_ref, |
| 1963 |
srcs: srcs, |
| 1964 |
dim: dim, |
| 1965 |
lod_mode: lod_mode, |
| 1966 |
is_ms: tex.op == nir_texop_txf_ms, |
| 1967 |
offset_mode, |
| 1968 |
mem_eviction_priority: MemEvictionPriority::Normal, |
| 1969 |
nodep: flags.nodep(), |
| 1970 |
channel_mask, |
| 1971 |
}); |
| 1972 |
} else if tex.op == nir_texop_tg4 { |
| 1973 |
b.push_op(OpTld4 { |
| 1974 |
dsts: dsts, |
| 1975 |
fault: fault.into(), |
| 1976 |
tex: tex_ref, |
| 1977 |
srcs: srcs, |
| 1978 |
dim: dim, |
| 1979 |
comp: tex.component().try_into().unwrap(), |
| 1980 |
offset_mode: offset_mode, |
| 1981 |
z_cmpr: flags.has_z_cmpr(), |
| 1982 |
mem_eviction_priority: MemEvictionPriority::Normal, |
| 1983 |
nodep: flags.nodep(), |
| 1984 |
channel_mask, |
| 1985 |
}); |
| 1986 |
} else { |
| 1987 |
assert!(offset_mode != TexOffsetMode::PerPx); |
| 1988 |
b.push_op(OpTex { |
| 1989 |
dsts: dsts, |
| 1990 |
fault: fault.into(), |
| 1991 |
tex: tex_ref, |
| 1992 |
srcs: srcs, |
| 1993 |
dim: dim, |
| 1994 |
lod_mode: lod_mode, |
| 1995 |
deriv_mode, |
| 1996 |
z_cmpr: flags.has_z_cmpr(), |
| 1997 |
offset_mode, |
| 1998 |
mem_eviction_priority: MemEvictionPriority::Normal, |
| 1999 |
nodep: flags.nodep(), |
| 2000 |
channel_mask, |
| 2001 |
}); |
| 2002 |
} |
| 2003 |
} |
| 2004 |
|
| 2005 |
let mut di = 0_usize; |
| 2006 |
let mut nir_dst = Vec::new(); |
| 2007 |
for i in 0..tex.def.num_components() { |
| 2008 |
if flags.is_sparse() && i == tex.def.num_components - 1 { |
| 2009 |
let Some(fault) = fault else { |
| 2010 |
panic!("No fault value for sparse op"); |
| 2011 |
}; |
| 2012 |
nir_dst.push(b.sel(fault.into(), 0.into(), 1.into())); |
| 2013 |
} else if mask & (1 << i) == 0 { |
| 2014 |
nir_dst.push(b.copy(0.into())); |
| 2015 |
} else { |
| 2016 |
nir_dst.push(dst[di]); |
| 2017 |
di += 1; |
| 2018 |
} |
| 2019 |
} |
| 2020 |
|
| 2021 |
self.set_ssa(tex.def.as_def(), nir_dst); |
| 2022 |
} |
| 2023 |
|
| 2024 |
fn get_atomic_type(&self, intrin: &nir_intrinsic_instr) -> AtomType { |
| 2025 |
let bit_size = intrin.def.bit_size(); |
| 2026 |
match intrin.atomic_op() { |
| 2027 |
nir_atomic_op_iadd => AtomType::U(bit_size), |
| 2028 |
nir_atomic_op_imin => AtomType::I(bit_size), |
| 2029 |
nir_atomic_op_umin => AtomType::U(bit_size), |
| 2030 |
nir_atomic_op_imax => AtomType::I(bit_size), |
| 2031 |
nir_atomic_op_umax => AtomType::U(bit_size), |
| 2032 |
nir_atomic_op_iand => AtomType::U(bit_size), |
| 2033 |
nir_atomic_op_ior => AtomType::U(bit_size), |
| 2034 |
nir_atomic_op_ixor => AtomType::U(bit_size), |
| 2035 |
nir_atomic_op_xchg => AtomType::U(bit_size), |
| 2036 |
nir_atomic_op_fadd => AtomType::F(bit_size), |
| 2037 |
nir_atomic_op_fmin => AtomType::F(bit_size), |
| 2038 |
nir_atomic_op_fmax => AtomType::F(bit_size), |
| 2039 |
nir_atomic_op_cmpxchg => AtomType::U(bit_size), |
| 2040 |
_ => panic!("Unsupported NIR atomic op"), |
| 2041 |
} |
| 2042 |
} |
| 2043 |
|
| 2044 |
fn get_atomic_op( |
| 2045 |
&self, |
| 2046 |
intrin: &nir_intrinsic_instr, |
| 2047 |
cmp_src: AtomCmpSrc, |
| 2048 |
) -> AtomOp { |
| 2049 |
match intrin.atomic_op() { |
| 2050 |
nir_atomic_op_iadd => AtomOp::Add, |
| 2051 |
nir_atomic_op_imin => AtomOp::Min, |
| 2052 |
nir_atomic_op_umin => AtomOp::Min, |
| 2053 |
nir_atomic_op_imax => AtomOp::Max, |
| 2054 |
nir_atomic_op_umax => AtomOp::Max, |
| 2055 |
nir_atomic_op_iand => AtomOp::And, |
| 2056 |
nir_atomic_op_ior => AtomOp::Or, |
| 2057 |
nir_atomic_op_ixor => AtomOp::Xor, |
| 2058 |
nir_atomic_op_xchg => AtomOp::Exch, |
| 2059 |
nir_atomic_op_fadd => AtomOp::Add, |
| 2060 |
nir_atomic_op_fmin => AtomOp::Min, |
| 2061 |
nir_atomic_op_fmax => AtomOp::Max, |
| 2062 |
nir_atomic_op_cmpxchg => AtomOp::CmpExch(cmp_src), |
| 2063 |
_ => panic!("Unsupported NIR atomic op"), |
| 2064 |
} |
| 2065 |
} |
| 2066 |
|
| 2067 |
fn get_eviction_priority( |
| 2068 |
&mut self, |
| 2069 |
access: gl_access_qualifier, |
| 2070 |
) -> MemEvictionPriority { |
| 2071 |
if self.sm.sm() >= 70 && access & ACCESS_NON_TEMPORAL != 0 { |
| 2072 |
MemEvictionPriority::First |
| 2073 |
} else { |
| 2074 |
MemEvictionPriority::Normal |
| 2075 |
} |
| 2076 |
} |
| 2077 |
|
| 2078 |
fn get_image_dim(&mut self, intrin: &nir_intrinsic_instr) -> ImageDim { |
| 2079 |
let is_array = intrin.image_array(); |
| 2080 |
let image_dim = intrin.image_dim(); |
| 2081 |
match intrin.image_dim() { |
| 2082 |
GLSL_SAMPLER_DIM_1D => { |
| 2083 |
if is_array { |
| 2084 |
ImageDim::_1DArray |
| 2085 |
} else { |
| 2086 |
ImageDim::_1D |
| 2087 |
} |
| 2088 |
} |
| 2089 |
GLSL_SAMPLER_DIM_2D => { |
| 2090 |
if is_array { |
| 2091 |
ImageDim::_2DArray |
| 2092 |
} else { |
| 2093 |
ImageDim::_2D |
| 2094 |
} |
| 2095 |
} |
| 2096 |
GLSL_SAMPLER_DIM_3D => { |
| 2097 |
assert!(!is_array); |
| 2098 |
ImageDim::_3D |
| 2099 |
} |
| 2100 |
GLSL_SAMPLER_DIM_CUBE => ImageDim::_2DArray, |
| 2101 |
GLSL_SAMPLER_DIM_BUF => { |
| 2102 |
assert!(!is_array); |
| 2103 |
ImageDim::_1DBuffer |
| 2104 |
} |
| 2105 |
_ => panic!("Unsupported image dimension: {}", image_dim), |
| 2106 |
} |
| 2107 |
} |
| 2108 |
|
| 2109 |
fn get_image_mem_type(&self, intrin: &nir_intrinsic_instr) -> MemType { |
| 2110 |
match intrin.format() { |
| 2111 |
PIPE_FORMAT_R8_UINT => MemType::U8, |
| 2112 |
PIPE_FORMAT_R8_SINT => MemType::I8, |
| 2113 |
PIPE_FORMAT_R16_UINT => MemType::U16, |
| 2114 |
PIPE_FORMAT_R16_SINT => MemType::I16, |
| 2115 |
PIPE_FORMAT_R32_UINT => MemType::B32, |
| 2116 |
PIPE_FORMAT_R32G32_UINT => MemType::B64, |
| 2117 |
PIPE_FORMAT_R32G32B32A32_UINT => MemType::B128, |
| 2118 |
_ => panic!("Unknown format"), |
| 2119 |
} |
| 2120 |
} |
| 2121 |
|
| 2122 |
fn get_image_coord( |
| 2123 |
&mut self, |
| 2124 |
intrin: &nir_intrinsic_instr, |
| 2125 |
dim: ImageDim, |
| 2126 |
) -> Src { |
| 2127 |
let vec = self.get_ssa(intrin.get_src(1).as_def()); |
| 2128 |
// let sample = self.get_src(&srcs[2]); |
| 2129 |
let comps = usize::from(dim.coord_comps()); |
| 2130 |
SSARef::try_from(&vec[0..comps]).unwrap().into() |
| 2131 |
} |
| 2132 |
|
| 2133 |
fn parse_intrinsic( |
| 2134 |
&mut self, |
| 2135 |
b: &mut impl SSABuilder, |
| 2136 |
intrin: &nir_intrinsic_instr, |
| 2137 |
) { |
| 2138 |
let srcs = intrin.srcs_as_slice(); |
| 2139 |
match intrin.intrinsic { |
| 2140 |
nir_intrinsic_al2p_nv => { |
| 2141 |
let offset = self.get_src(&srcs[0]); |
| 2142 |
let addr = u16::try_from(intrin.base()).unwrap(); |
| 2143 |
|
| 2144 |
let flags = intrin.flags(); |
| 2145 |
let flags: nak_nir_attr_io_flags = |
| 2146 |
unsafe { std::mem::transmute_copy(&flags) }; |
| 2147 |
|
| 2148 |
assert!(!flags.patch()); |
| 2149 |
|
| 2150 |
let dst = b.alloc_ssa(RegFile::GPR); |
| 2151 |
b.push_op(OpAL2P { |
| 2152 |
dst: dst.into(), |
| 2153 |
offset, |
| 2154 |
addr, |
| 2155 |
output: flags.output(), |
| 2156 |
comps: 1, |
| 2157 |
}); |
| 2158 |
self.set_dst(&intrin.def, dst.into()); |
| 2159 |
} |
| 2160 |
nir_intrinsic_ald_nv | nir_intrinsic_ast_nv => { |
| 2161 |
let addr = u16::try_from(intrin.base()).unwrap(); |
| 2162 |
let base = u16::try_from(intrin.range_base()).unwrap(); |
| 2163 |
let range = u16::try_from(intrin.range()).unwrap(); |
| 2164 |
let range = base..(base + range); |
| 2165 |
|
| 2166 |
let flags = intrin.flags(); |
| 2167 |
let flags: nak_nir_attr_io_flags = |
| 2168 |
unsafe { std::mem::transmute_copy(&flags) }; |
| 2169 |
assert!(!flags.patch() || !flags.phys()); |
| 2170 |
|
| 2171 |
if let ShaderIoInfo::Vtg(io) = &mut self.info.io { |
| 2172 |
if flags.patch() { |
| 2173 |
match &mut self.info.stage { |
| 2174 |
ShaderStageInfo::TessellationInit(stage) => { |
| 2175 |
assert!(flags.output()); |
| 2176 |
stage.per_patch_attribute_count = max( |
| 2177 |
stage.per_patch_attribute_count, |
| 2178 |
(range.end / 4).try_into().unwrap(), |
| 2179 |
); |
| 2180 |
} |
| 2181 |
ShaderStageInfo::Tessellation(_) => (), |
| 2182 |
_ => panic!("Patch I/O not supported"), |
| 2183 |
} |
| 2184 |
} else { |
| 2185 |
if flags.output() { |
| 2186 |
if intrin.intrinsic == nir_intrinsic_ast_nv { |
| 2187 |
io.mark_store_req(range.clone()); |
| 2188 |
} |
| 2189 |
io.mark_attrs_written(range); |
| 2190 |
} else { |
| 2191 |
io.mark_attrs_read(range); |
| 2192 |
} |
| 2193 |
} |
| 2194 |
} else { |
| 2195 |
panic!("Must be a VTG stage"); |
| 2196 |
} |
| 2197 |
|
| 2198 |
let comps = intrin.num_components; |
| 2199 |
|
| 2200 |
if intrin.intrinsic == nir_intrinsic_ald_nv { |
| 2201 |
let vtx = self.get_src(&srcs[0]); |
| 2202 |
let offset = self.get_src(&srcs[1]); |
| 2203 |
|
| 2204 |
assert!(intrin.def.bit_size() == 32); |
| 2205 |
let dst = b.alloc_ssa_vec(RegFile::GPR, comps); |
| 2206 |
b.push_op(OpALd { |
| 2207 |
dst: dst.clone().into(), |
| 2208 |
vtx, |
| 2209 |
addr, |
| 2210 |
offset, |
| 2211 |
comps, |
| 2212 |
patch: flags.patch(), |
| 2213 |
output: flags.output(), |
| 2214 |
phys: flags.phys(), |
| 2215 |
}); |
| 2216 |
self.set_dst(&intrin.def, dst); |
| 2217 |
} else if intrin.intrinsic == nir_intrinsic_ast_nv { |
| 2218 |
assert!(srcs[0].bit_size() == 32); |
| 2219 |
let data = self.get_src(&srcs[0]); |
| 2220 |
let vtx = self.get_src(&srcs[1]); |
| 2221 |
let offset = self.get_src(&srcs[2]); |
| 2222 |
|
| 2223 |
b.push_op(OpASt { |
| 2224 |
data, |
| 2225 |
vtx, |
| 2226 |
addr, |
| 2227 |
offset, |
| 2228 |
comps, |
| 2229 |
patch: flags.patch(), |
| 2230 |
phys: flags.phys(), |
| 2231 |
}); |
| 2232 |
} else { |
| 2233 |
panic!("Invalid VTG I/O intrinsic"); |
| 2234 |
} |
| 2235 |
} |
| 2236 |
nir_intrinsic_as_uniform => { |
| 2237 |
let src = self.get_ssa(srcs[0].as_def()); |
| 2238 |
let mut dst = Vec::new(); |
| 2239 |
for comp in src { |
| 2240 |
let u = b.alloc_ssa(comp.file().to_uniform().unwrap()); |
| 2241 |
b.push_op(OpR2UR { |
| 2242 |
src: [*comp].into(), |
| 2243 |
dst: u.into(), |
| 2244 |
}); |
| 2245 |
dst.push(u); |
| 2246 |
} |
| 2247 |
self.set_ssa(&intrin.def, dst); |
| 2248 |
} |
| 2249 |
nir_intrinsic_convert_alu_types => { |
| 2250 |
let src_base_type = intrin.src_type().base_type(); |
| 2251 |
let src_bit_size = intrin.src_type().bit_size(); |
| 2252 |
let dst_base_type = intrin.dest_type().base_type(); |
| 2253 |
let dst_bit_size = intrin.dest_type().bit_size(); |
| 2254 |
let rnd_mode = f_rnd_mode_from_nir(intrin.rounding_mode()); |
| 2255 |
|
| 2256 |
assert!(srcs[0].as_def().bit_size() == src_bit_size); |
| 2257 |
assert!(intrin.def.bit_size() == dst_bit_size); |
| 2258 |
|
| 2259 |
let def_bits = |
| 2260 |
intrin.def.bit_size() * intrin.def.num_components(); |
| 2261 |
let dst = b.alloc_ssa_vec(RegFile::GPR, def_bits.div_ceil(32)); |
| 2262 |
|
| 2263 |
match dst_base_type { |
| 2264 |
ALUType::INT | ALUType::UINT => { |
| 2265 |
let dst_type = IntType::from_bits( |
| 2266 |
dst_bit_size.into(), |
| 2267 |
dst_base_type == ALUType::INT, |
| 2268 |
); |
| 2269 |
match src_base_type { |
| 2270 |
ALUType::INT | ALUType::UINT => { |
| 2271 |
let src_type = IntType::from_bits( |
| 2272 |
src_bit_size.into(), |
| 2273 |
src_base_type == ALUType::INT, |
| 2274 |
); |
| 2275 |
b.push_op(OpI2I { |
| 2276 |
dst: dst.clone().into(), |
| 2277 |
src: self.get_src(&srcs[0]), |
| 2278 |
src_type, |
| 2279 |
dst_type, |
| 2280 |
abs: false, |
| 2281 |
neg: false, |
| 2282 |
saturate: intrin.saturate(), |
| 2283 |
}); |
| 2284 |
} |
| 2285 |
ALUType::FLOAT => { |
| 2286 |
let src_type = |
| 2287 |
FloatType::from_bits(src_bit_size.into()); |
| 2288 |
// F2I doesn't support 8-bit destinations |
| 2289 |
// pre-Volta |
| 2290 |
assert!(b.sm() >= 70 || dst_bit_size > 8); |
| 2291 |
b.push_op(OpF2I { |
| 2292 |
dst: dst.clone().into(), |
| 2293 |
src: self.get_src(&srcs[0]), |
| 2294 |
src_type, |
| 2295 |
dst_type, |
| 2296 |
rnd_mode, |
| 2297 |
ftz: self.float_ctl[src_type].ftz, |
| 2298 |
}); |
| 2299 |
} |
| 2300 |
_ => panic!("Unknown src_type"), |
| 2301 |
} |
| 2302 |
} |
| 2303 |
ALUType::FLOAT => { |
| 2304 |
let dst_type = |
| 2305 |
FloatType::from_bits(dst_bit_size.into()); |
| 2306 |
match src_base_type { |
| 2307 |
ALUType::INT | ALUType::UINT => { |
| 2308 |
let src_type = IntType::from_bits( |
| 2309 |
src_bit_size.into(), |
| 2310 |
src_base_type == ALUType::INT, |
| 2311 |
); |
| 2312 |
b.push_op(OpI2F { |
| 2313 |
dst: dst.clone().into(), |
| 2314 |
src: self.get_src(&srcs[0]), |
| 2315 |
src_type, |
| 2316 |
dst_type, |
| 2317 |
rnd_mode, |
| 2318 |
}); |
| 2319 |
} |
| 2320 |
ALUType::FLOAT => { |
| 2321 |
let src_type = |
| 2322 |
FloatType::from_bits(src_bit_size.into()); |
| 2323 |
b.push_op(OpF2F { |
| 2324 |
dst: dst.clone().into(), |
| 2325 |
src: self.get_src(&srcs[0]), |
| 2326 |
src_type, |
| 2327 |
dst_type, |
| 2328 |
rnd_mode, |
| 2329 |
ftz: self.float_ctl[src_type].ftz, |
| 2330 |
high: false, |
| 2331 |
integer_rnd: false, |
| 2332 |
}); |
| 2333 |
} |
| 2334 |
_ => panic!("Unknown src_type"), |
| 2335 |
} |
| 2336 |
} |
| 2337 |
_ => panic!("Unknown dest_type"), |
| 2338 |
} |
| 2339 |
self.set_dst(&intrin.def, dst); |
| 2340 |
} |
| 2341 |
nir_intrinsic_ddx |
| 2342 |
| nir_intrinsic_ddx_coarse |
| 2343 |
| nir_intrinsic_ddx_fine => { |
| 2344 |
// TODO: Real coarse derivatives |
| 2345 |
|
| 2346 |
assert!(intrin.def.bit_size() == 32); |
| 2347 |
let ftype = FloatType::F32; |
| 2348 |
|
| 2349 |
let dst = b.alloc_ssa(RegFile::GPR); |
| 2350 |
|
| 2351 |
if self.sm.sm() >= 50 { |
| 2352 |
let scratch = b.alloc_ssa(RegFile::GPR); |
| 2353 |
|
| 2354 |
b.push_op(OpShfl { |
| 2355 |
dst: scratch.into(), |
| 2356 |
in_bounds: Dst::None, |
| 2357 |
src: self.get_src(&srcs[0]), |
| 2358 |
lane: 1_u32.into(), |
| 2359 |
c: (0x3_u32 | (0x1c_u32 << 8)).into(), |
| 2360 |
op: ShflOp::Bfly, |
| 2361 |
}); |
| 2362 |
|
| 2363 |
// Starting with Blackwell, the shader stage now affects |
| 2364 |
// fswzadd so we need to use fswzadd.ndv |
| 2365 |
let deriv_mode = if self.sm.sm() >= 100 { |
| 2366 |
TexDerivMode::NonDivergent |
| 2367 |
} else { |
| 2368 |
TexDerivMode::Auto |
| 2369 |
}; |
| 2370 |
|
| 2371 |
b.push_op(OpFSwzAdd { |
| 2372 |
dst: dst.into(), |
| 2373 |
srcs: [scratch.into(), self.get_src(&srcs[0])], |
| 2374 |
ops: [ |
| 2375 |
FSwzAddOp::SubLeft, |
| 2376 |
FSwzAddOp::SubRight, |
| 2377 |
FSwzAddOp::SubLeft, |
| 2378 |
FSwzAddOp::SubRight, |
| 2379 |
], |
| 2380 |
rnd_mode: self.float_ctl[ftype].rnd_mode, |
| 2381 |
ftz: self.float_ctl[ftype].ftz, |
| 2382 |
deriv_mode, |
| 2383 |
}); |
| 2384 |
} else { |
| 2385 |
b.push_op(OpFSwz { |
| 2386 |
dst: dst.into(), |
| 2387 |
srcs: [self.get_src(&srcs[0]), self.get_src(&srcs[0])], |
| 2388 |
ops: [ |
| 2389 |
FSwzAddOp::SubLeft, |
| 2390 |
FSwzAddOp::SubRight, |
| 2391 |
FSwzAddOp::SubLeft, |
| 2392 |
FSwzAddOp::SubRight, |
| 2393 |
], |
| 2394 |
rnd_mode: self.float_ctl[ftype].rnd_mode, |
| 2395 |
ftz: self.float_ctl[ftype].ftz, |
| 2396 |
deriv_mode: TexDerivMode::Auto, |
| 2397 |
shuffle: FSwzShuffle::SwapHorizontal, |
| 2398 |
}); |
| 2399 |
} |
| 2400 |
|
| 2401 |
self.set_dst(&intrin.def, dst.into()); |
| 2402 |
} |
| 2403 |
nir_intrinsic_ddy |
| 2404 |
| nir_intrinsic_ddy_coarse |
| 2405 |
| nir_intrinsic_ddy_fine => { |
| 2406 |
// TODO: Real coarse derivatives |
| 2407 |
|
| 2408 |
assert!(intrin.def.bit_size() == 32); |
| 2409 |
let ftype = FloatType::F32; |
| 2410 |
let dst = b.alloc_ssa(RegFile::GPR); |
| 2411 |
|
| 2412 |
if self.sm.sm() >= 50 { |
| 2413 |
let scratch = b.alloc_ssa(RegFile::GPR); |
| 2414 |
|
| 2415 |
b.push_op(OpShfl { |
| 2416 |
dst: scratch.into(), |
| 2417 |
in_bounds: Dst::None, |
| 2418 |
src: self.get_src(&srcs[0]), |
| 2419 |
lane: 2_u32.into(), |
| 2420 |
c: (0x3_u32 | (0x1c_u32 << 8)).into(), |
| 2421 |
op: ShflOp::Bfly, |
| 2422 |
}); |
| 2423 |
|
| 2424 |
// Starting with Blackwell, the shader stage now affects |
| 2425 |
// fswzadd so we need to use fswzadd.ndv |
| 2426 |
let deriv_mode = if self.sm.sm() >= 100 { |
| 2427 |
TexDerivMode::NonDivergent |
| 2428 |
} else { |
| 2429 |
TexDerivMode::Auto |
| 2430 |
}; |
| 2431 |
|
| 2432 |
b.push_op(OpFSwzAdd { |
| 2433 |
dst: dst.into(), |
| 2434 |
srcs: [scratch.into(), self.get_src(&srcs[0])], |
| 2435 |
ops: [ |
| 2436 |
FSwzAddOp::SubLeft, |
| 2437 |
FSwzAddOp::SubLeft, |
| 2438 |
FSwzAddOp::SubRight, |
| 2439 |
FSwzAddOp::SubRight, |
| 2440 |
], |
| 2441 |
rnd_mode: self.float_ctl[ftype].rnd_mode, |
| 2442 |
ftz: self.float_ctl[ftype].ftz, |
| 2443 |
deriv_mode, |
| 2444 |
}); |
| 2445 |
} else { |
| 2446 |
b.push_op(OpFSwz { |
| 2447 |
dst: dst.into(), |
| 2448 |
srcs: [self.get_src(&srcs[0]), self.get_src(&srcs[0])], |
| 2449 |
ops: [ |
| 2450 |
FSwzAddOp::SubLeft, |
| 2451 |
FSwzAddOp::SubLeft, |
| 2452 |
FSwzAddOp::SubRight, |
| 2453 |
FSwzAddOp::SubRight, |
| 2454 |
], |
| 2455 |
rnd_mode: self.float_ctl[ftype].rnd_mode, |
| 2456 |
ftz: self.float_ctl[ftype].ftz, |
| 2457 |
deriv_mode: TexDerivMode::Auto, |
| 2458 |
shuffle: FSwzShuffle::SwapVertical, |
| 2459 |
}); |
| 2460 |
} |
| 2461 |
|
| 2462 |
self.set_dst(&intrin.def, dst.into()); |
| 2463 |
} |
| 2464 |
nir_intrinsic_ballot => { |
| 2465 |
assert!(srcs[0].bit_size() == 1); |
| 2466 |
let src = self.get_src(&srcs[0]); |
| 2467 |
|
| 2468 |
assert!(intrin.def.bit_size() == 32); |
| 2469 |
let dst = b.alloc_ssa(RegFile::GPR); |
| 2470 |
|
| 2471 |
b.push_op(OpVote { |
| 2472 |
op: VoteOp::Any, |
| 2473 |
ballot: dst.into(), |
| 2474 |
vote: Dst::None, |
| 2475 |
pred: src, |
| 2476 |
}); |
| 2477 |
self.set_dst(&intrin.def, dst.into()); |
| 2478 |
} |
| 2479 |
nir_intrinsic_bar_break_nv => { |
| 2480 |
let src = self.get_src(&srcs[0]); |
| 2481 |
let bar_in = b.bmov_to_bar(src); |
| 2482 |
let cond = self.get_src(&srcs[1]); |
| 2483 |
|
| 2484 |
let bar_out = b.alloc_ssa(RegFile::Bar); |
| 2485 |
b.push_op(OpBreak { |
| 2486 |
bar_out: bar_out.into(), |
| 2487 |
bar_in: bar_in.into(), |
| 2488 |
cond, |
| 2489 |
}); |
| 2490 |
|
| 2491 |
self.set_dst(&intrin.def, b.bmov_to_gpr(bar_out.into()).into()); |
| 2492 |
} |
| 2493 |
nir_intrinsic_bar_set_nv => { |
| 2494 |
let label = self.label_alloc.alloc(); |
| 2495 |
let old = self.bar_label.insert(intrin.def.index, label); |
| 2496 |
assert!(old.is_none()); |
| 2497 |
|
| 2498 |
let bar_clear = b.alloc_ssa(RegFile::Bar); |
| 2499 |
b.push_op(OpBClear { |
| 2500 |
dst: bar_clear.into(), |
| 2501 |
}); |
| 2502 |
|
| 2503 |
let bar_out = b.alloc_ssa(RegFile::Bar); |
| 2504 |
b.push_op(OpBSSy { |
| 2505 |
bar_out: bar_out.into(), |
| 2506 |
bar_in: bar_clear.into(), |
| 2507 |
cond: SrcRef::True.into(), |
| 2508 |
target: label, |
| 2509 |
}); |
| 2510 |
|
| 2511 |
self.set_dst(&intrin.def, b.bmov_to_gpr(bar_out.into()).into()); |
| 2512 |
} |
| 2513 |
nir_intrinsic_bar_sync_nv => { |
| 2514 |
let src = self.get_src(&srcs[0]); |
| 2515 |
|
| 2516 |
let bar = b.bmov_to_bar(src); |
| 2517 |
b.push_op(OpBSync { |
| 2518 |
bar: bar.into(), |
| 2519 |
cond: SrcRef::True.into(), |
| 2520 |
}); |
| 2521 |
|
| 2522 |
let bar_set_idx = &srcs[1].as_def().index; |
| 2523 |
if let Some(label) = self.bar_label.get(bar_set_idx) { |
| 2524 |
b.push_op(OpNop { |
| 2525 |
label: Some(*label), |
| 2526 |
}); |
| 2527 |
} |
| 2528 |
} |
| 2529 |
nir_intrinsic_bindless_image_atomic |
| 2530 |
| nir_intrinsic_bindless_image_atomic_swap => { |
| 2531 |
let handle = self.get_src(&srcs[0]); |
| 2532 |
let dim = self.get_image_dim(intrin); |
| 2533 |
let coord = self.get_image_coord(intrin, dim); |
| 2534 |
// let sample = self.get_src(&srcs[2]); |
| 2535 |
let atom_type = self.get_atomic_type(intrin); |
| 2536 |
let atom_op = self.get_atomic_op(intrin, AtomCmpSrc::Packed); |
| 2537 |
|
| 2538 |
assert!( |
| 2539 |
intrin.def.bit_size() == 32 || intrin.def.bit_size() == 64 |
| 2540 |
); |
| 2541 |
assert!(intrin.def.num_components() == 1); |
| 2542 |
let dst = |
| 2543 |
b.alloc_ssa_vec(RegFile::GPR, intrin.def.bit_size() / 32); |
| 2544 |
|
| 2545 |
let data = if intrin.intrinsic |
| 2546 |
== nir_intrinsic_bindless_image_atomic_swap |
| 2547 |
{ |
| 2548 |
if intrin.def.bit_size() == 64 { |
| 2549 |
SSARef::from([ |
| 2550 |
self.get_ssa(srcs[3].as_def())[0], |
| 2551 |
self.get_ssa(srcs[3].as_def())[1], |
| 2552 |
self.get_ssa(srcs[4].as_def())[0], |
| 2553 |
self.get_ssa(srcs[4].as_def())[1], |
| 2554 |
]) |
| 2555 |
.into() |
| 2556 |
} else { |
| 2557 |
SSARef::from([ |
| 2558 |
self.get_ssa(srcs[3].as_def())[0], |
| 2559 |
self.get_ssa(srcs[4].as_def())[0], |
| 2560 |
]) |
| 2561 |
.into() |
| 2562 |
} |
| 2563 |
} else { |
| 2564 |
self.get_src(&srcs[3]) |
| 2565 |
}; |
| 2566 |
|
| 2567 |
let is_reduction = |
| 2568 |
atom_op.is_reduction() && intrin.def.components_read() == 0; |
| 2569 |
|
| 2570 |
b.push_op(OpSuAtom { |
| 2571 |
dst: if self.sm.sm() >= 70 && is_reduction { |
| 2572 |
Dst::None |
| 2573 |
} else { |
| 2574 |
dst.clone().into() |
| 2575 |
}, |
| 2576 |
fault: Dst::None, |
| 2577 |
handle: handle, |
| 2578 |
coord: coord, |
| 2579 |
data: data, |
| 2580 |
atom_op: atom_op, |
| 2581 |
atom_type: atom_type, |
| 2582 |
image_dim: dim, |
| 2583 |
mem_order: MemOrder::Strong(MemScope::GPU), |
| 2584 |
mem_eviction_priority: self |
| 2585 |
.get_eviction_priority(intrin.access()), |
| 2586 |
}); |
| 2587 |
self.set_dst(&intrin.def, dst); |
| 2588 |
} |
| 2589 |
nir_intrinsic_suclamp_nv => { |
| 2590 |
let coords = self.get_src(&srcs[0]); |
| 2591 |
let params = self.get_src(&srcs[1]); |
| 2592 |
|
| 2593 |
let flags = intrin.flags(); |
| 2594 |
let flags: nak_nir_suclamp_flags = |
| 2595 |
unsafe { std::mem::transmute_copy(&flags) }; |
| 2596 |
|
| 2597 |
let mode = match flags.mode() { |
| 2598 |
NAK_SUCLAMP_MODE_BLOCK_LINEAR => SuClampMode::BlockLinear, |
| 2599 |
NAK_SUCLAMP_MODE_PITCH_LINEAR => SuClampMode::PitchLinear, |
| 2600 |
NAK_SUCLAMP_MODE_STORED_DESCRIPTOR => { |
| 2601 |
SuClampMode::StoredInDescriptor |
| 2602 |
} |
| 2603 |
_ => panic!("Invalid suclamp mode"), |
| 2604 |
}; |
| 2605 |
|
| 2606 |
let round = match flags.round() { |
| 2607 |
NAK_SUCLAMP_ROUND_R1 => SuClampRound::R1, |
| 2608 |
NAK_SUCLAMP_ROUND_R2 => SuClampRound::R2, |
| 2609 |
NAK_SUCLAMP_ROUND_R4 => SuClampRound::R4, |
| 2610 |
NAK_SUCLAMP_ROUND_R8 => SuClampRound::R8, |
| 2611 |
NAK_SUCLAMP_ROUND_R16 => SuClampRound::R16, |
| 2612 |
_ => panic!("Invalid suclamp round"), |
| 2613 |
}; |
| 2614 |
|
| 2615 |
let dst = b.alloc_ssa(RegFile::GPR); |
| 2616 |
let out_of_bounds = b.alloc_ssa(RegFile::Pred); |
| 2617 |
b.push_op(OpSuClamp { |
| 2618 |
dst: dst.into(), |
| 2619 |
out_of_bounds: out_of_bounds.into(), |
| 2620 |
coords, |
| 2621 |
params, |
| 2622 |
mode, |
| 2623 |
round, |
| 2624 |
is_2d: flags.is_2d(), |
| 2625 |
is_s32: flags.is_s32(), |
| 2626 |
imm: 0, |
| 2627 |
}); |
| 2628 |
let final_dst = |
| 2629 |
vec![dst, b.sel(out_of_bounds.into(), 1.into(), 0.into())]; |
| 2630 |
|
| 2631 |
self.set_ssa(&intrin.def, final_dst); |
| 2632 |
} |
| 2633 |
nir_intrinsic_subfm_nv => { |
| 2634 |
let x = self.get_src(&srcs[0]); |
| 2635 |
let y = self.get_src(&srcs[1]); |
| 2636 |
let z = self.get_src(&srcs[2]); |
| 2637 |
let is_3d = intrin.flags() != 0; |
| 2638 |
|
| 2639 |
let dst = b.alloc_ssa(RegFile::GPR); |
| 2640 |
let out_of_bounds = b.alloc_ssa(RegFile::Pred); |
| 2641 |
b.push_op(OpSuBfm { |
| 2642 |
dst: dst.into(), |
| 2643 |
pdst: out_of_bounds.into(), |
| 2644 |
srcs: [x, y, z], |
| 2645 |
is_3d, |
| 2646 |
}); |
| 2647 |
let final_dst = |
| 2648 |
vec![dst, b.sel(out_of_bounds.into(), 1.into(), 0.into())]; |
| 2649 |
|
| 2650 |
self.set_ssa(&intrin.def, final_dst); |
| 2651 |
} |
| 2652 |
nir_intrinsic_sueau_nv => { |
| 2653 |
let off = self.get_src(&srcs[0]); |
| 2654 |
let bit_field = self.get_src(&srcs[1]); |
| 2655 |
let addr = self.get_src(&srcs[2]); |
| 2656 |
|
| 2657 |
let dst = b.alloc_ssa(RegFile::GPR); |
| 2658 |
b.push_op(OpSuEau { |
| 2659 |
dst: dst.into(), |
| 2660 |
off, |
| 2661 |
bit_field, |
| 2662 |
addr, |
| 2663 |
}); |
| 2664 |
self.set_dst(&intrin.def, dst.into()); |
| 2665 |
} |
| 2666 |
nir_intrinsic_imadsp_nv => { |
| 2667 |
let src0 = self.get_src(&srcs[0]); |
| 2668 |
let src1 = self.get_src(&srcs[1]); |
| 2669 |
let src2 = self.get_src(&srcs[2]); |
| 2670 |
|
| 2671 |
let flags = intrin.flags(); |
| 2672 |
let flags: nak_nir_imadsp_flags = |
| 2673 |
unsafe { std::mem::transmute_copy(&flags) }; |
| 2674 |
|
| 2675 |
let translate_src_type = |s| { |
| 2676 |
use IMadSpSrcType::*; |
| 2677 |
match s { |
| 2678 |
NAK_IMAD_TYPE_U32 => U32, |
| 2679 |
NAK_IMAD_TYPE_U24 => U24, |
| 2680 |
NAK_IMAD_TYPE_U16_LO => U16Lo, |
| 2681 |
NAK_IMAD_TYPE_U16_HI => U16Hi, |
| 2682 |
NAK_IMAD_TYPE_S32 => S32, |
| 2683 |
NAK_IMAD_TYPE_S24 => S24, |
| 2684 |
NAK_IMAD_TYPE_S16_LO => S16Lo, |
| 2685 |
NAK_IMAD_TYPE_S16_HI => S16Hi, |
| 2686 |
_ => panic!("Invalid imadsp mode"), |
| 2687 |
} |
| 2688 |
}; |
| 2689 |
|
| 2690 |
let mode = if flags.params_from_src1() { |
| 2691 |
IMadSpMode::FromSrc1 |
| 2692 |
} else { |
| 2693 |
IMadSpMode::Explicit([ |
| 2694 |
translate_src_type(flags.src0()), |
| 2695 |
translate_src_type(flags.src1()), |
| 2696 |
translate_src_type(flags.src2()), |
| 2697 |
]) |
| 2698 |
}; |
| 2699 |
|
| 2700 |
let dst = b.alloc_ssa(RegFile::GPR); |
| 2701 |
b.push_op(OpIMadSp { |
| 2702 |
srcs: [src0, src1, src2], |
| 2703 |
dst: dst.into(), |
| 2704 |
mode, |
| 2705 |
}); |
| 2706 |
self.set_dst(&intrin.def, dst.into()); |
| 2707 |
} |
| 2708 |
nir_intrinsic_suldga_nv => { |
| 2709 |
let addr = self.get_src(&srcs[0]); |
| 2710 |
let format = self.get_src(&srcs[1]); |
| 2711 |
let out_of_bounds = self.get_src(&srcs[2]); |
| 2712 |
|
| 2713 |
let comps = intrin.num_components; |
| 2714 |
|
| 2715 |
assert!(intrin.def.bit_size() == 32); |
| 2716 |
let mem_type = self.get_image_mem_type(intrin); |
| 2717 |
|
| 2718 |
let flags = intrin.flags(); |
| 2719 |
let offset_mode = match flags { |
| 2720 |
NAK_SUGA_OFF_MODE_U32 => SuGaOffsetMode::U32, |
| 2721 |
NAK_SUGA_OFF_MODE_S32 => SuGaOffsetMode::S32, |
| 2722 |
NAK_SUGA_OFF_MODE_U8 => SuGaOffsetMode::U8, |
| 2723 |
NAK_SUGA_OFF_MODE_S8 => SuGaOffsetMode::S8, |
| 2724 |
_ => panic!("Invalid suldga flags"), |
| 2725 |
}; |
| 2726 |
|
| 2727 |
let mem_order = if (intrin.access() & ACCESS_CAN_REORDER) != 0 { |
| 2728 |
MemOrder::Constant |
| 2729 |
} else { |
| 2730 |
MemOrder::Strong(MemScope::GPU) |
| 2731 |
}; |
| 2732 |
let cache_op = LdCacheOp::select( |
| 2733 |
self.sm, |
| 2734 |
MemSpace::Global(MemAddrType::A64), |
| 2735 |
mem_order, |
| 2736 |
self.get_eviction_priority(intrin.access()), |
| 2737 |
); |
| 2738 |
|
| 2739 |
let dst = b.alloc_ssa_vec(RegFile::GPR, comps); |
| 2740 |
b.push_op(OpSuLdGa { |
| 2741 |
dst: dst.clone().into(), |
| 2742 |
addr, |
| 2743 |
format, |
| 2744 |
out_of_bounds, |
| 2745 |
mem_type, |
| 2746 |
offset_mode, |
| 2747 |
cache_op, |
| 2748 |
}); |
| 2749 |
self.set_dst(&intrin.def, dst); |
| 2750 |
} |
| 2751 |
nir_intrinsic_bindless_image_load => { |
| 2752 |
let handle = self.get_src(&srcs[0]); |
| 2753 |
let dim = self.get_image_dim(intrin); |
| 2754 |
let coord = self.get_image_coord(intrin, dim); |
| 2755 |
// let sample = self.get_src(&srcs[2]); |
| 2756 |
|
| 2757 |
let mem_order = if (intrin.access() & ACCESS_CAN_REORDER) != 0 { |
| 2758 |
MemOrder::Constant |
| 2759 |
} else { |
| 2760 |
MemOrder::Strong(MemScope::GPU) |
| 2761 |
}; |
| 2762 |
|
| 2763 |
let comps = intrin.num_components; |
| 2764 |
assert!(intrin.def.bit_size() == 32); |
| 2765 |
assert!(comps == 1 || comps == 2 || comps == 4); |
| 2766 |
let image_access = |
| 2767 |
ImageAccess::Formatted(ChannelMask::for_comps(comps)); |
| 2768 |
|
| 2769 |
let dst = b.alloc_ssa_vec(RegFile::GPR, comps); |
| 2770 |
|
| 2771 |
b.push_op(OpSuLd { |
| 2772 |
dst: dst.clone().into(), |
| 2773 |
fault: Dst::None, |
| 2774 |
image_access, |
| 2775 |
image_dim: dim, |
| 2776 |
mem_order, |
| 2777 |
mem_eviction_priority: self |
| 2778 |
.get_eviction_priority(intrin.access()), |
| 2779 |
handle: handle, |
| 2780 |
coord: coord, |
| 2781 |
}); |
| 2782 |
self.set_dst(&intrin.def, dst); |
| 2783 |
} |
| 2784 |
nir_intrinsic_bindless_image_sparse_load => { |
| 2785 |
let handle = self.get_src(&srcs[0]); |
| 2786 |
let dim = self.get_image_dim(intrin); |
| 2787 |
let coord = self.get_image_coord(intrin, dim); |
| 2788 |
// let sample = self.get_src(&srcs[2]); |
| 2789 |
|
| 2790 |
let mem_order = if (intrin.access() & ACCESS_CAN_REORDER) != 0 { |
| 2791 |
MemOrder::Constant |
| 2792 |
} else { |
| 2793 |
MemOrder::Strong(MemScope::GPU) |
| 2794 |
}; |
| 2795 |
|
| 2796 |
let comps = intrin.num_components; |
| 2797 |
assert!(intrin.def.bit_size() == 32); |
| 2798 |
assert!(comps == 2 || comps == 3 || comps == 5); |
| 2799 |
let image_access = |
| 2800 |
ImageAccess::Formatted(ChannelMask::for_comps(comps - 1)); |
| 2801 |
|
| 2802 |
let dst = b.alloc_ssa_vec(RegFile::GPR, comps - 1); |
| 2803 |
let fault = b.alloc_ssa(RegFile::Pred); |
| 2804 |
|
| 2805 |
b.push_op(OpSuLd { |
| 2806 |
dst: dst.clone().into(), |
| 2807 |
fault: fault.into(), |
| 2808 |
image_access, |
| 2809 |
image_dim: dim, |
| 2810 |
mem_order, |
| 2811 |
mem_eviction_priority: self |
| 2812 |
.get_eviction_priority(intrin.access()), |
| 2813 |
handle: handle, |
| 2814 |
coord: coord, |
| 2815 |
}); |
| 2816 |
|
| 2817 |
let mut final_dst = Vec::new(); |
| 2818 |
for i in 0..usize::from(comps) - 1 { |
| 2819 |
final_dst.push(dst[i]); |
| 2820 |
} |
| 2821 |
final_dst.push(b.sel(fault.into(), 0.into(), 1.into())); |
| 2822 |
|
| 2823 |
self.set_ssa(&intrin.def, final_dst); |
| 2824 |
} |
| 2825 |
nir_intrinsic_sustga_nv => { |
| 2826 |
let addr = self.get_src(&srcs[0]); |
| 2827 |
let format = self.get_src(&srcs[1]); |
| 2828 |
let out_of_bounds = self.get_src(&srcs[2]); |
| 2829 |
|
| 2830 |
let data = self.get_src(&srcs[3]); |
| 2831 |
let image_access = |
| 2832 |
ImageAccess::Formatted(ChannelMask::new(0xf)); |
| 2833 |
|
| 2834 |
let flags = intrin.flags(); |
| 2835 |
let offset_mode = match flags { |
| 2836 |
NAK_SUGA_OFF_MODE_U32 => SuGaOffsetMode::U32, |
| 2837 |
NAK_SUGA_OFF_MODE_S32 => SuGaOffsetMode::S32, |
| 2838 |
NAK_SUGA_OFF_MODE_U8 => SuGaOffsetMode::U8, |
| 2839 |
NAK_SUGA_OFF_MODE_S8 => SuGaOffsetMode::S8, |
| 2840 |
_ => panic!("Invalid sustga flags"), |
| 2841 |
}; |
| 2842 |
|
| 2843 |
let cache_op = StCacheOp::select( |
| 2844 |
self.sm, |
| 2845 |
MemSpace::Global(MemAddrType::A64), |
| 2846 |
MemOrder::Strong(MemScope::GPU), |
| 2847 |
self.get_eviction_priority(intrin.access()), |
| 2848 |
); |
| 2849 |
|
| 2850 |
b.push_op(OpSuStGa { |
| 2851 |
addr, |
| 2852 |
format, |
| 2853 |
data, |
| 2854 |
out_of_bounds, |
| 2855 |
image_access, |
| 2856 |
offset_mode, |
| 2857 |
cache_op, |
| 2858 |
}); |
| 2859 |
} |
| 2860 |
nir_intrinsic_bindless_image_store => { |
| 2861 |
let handle = self.get_src(&srcs[0]); |
| 2862 |
let dim = self.get_image_dim(intrin); |
| 2863 |
let coord = self.get_image_coord(intrin, dim); |
| 2864 |
// let sample = self.get_src(&srcs[2]); |
| 2865 |
let data = self.get_src(&srcs[3]); |
| 2866 |
|
| 2867 |
let comps = intrin.num_components; |
| 2868 |
assert!(srcs[3].bit_size() == 32); |
| 2869 |
assert!(comps == 1 || comps == 2 || comps == 4); |
| 2870 |
let image_access = |
| 2871 |
ImageAccess::Formatted(ChannelMask::for_comps(comps)); |
| 2872 |
|
| 2873 |
b.push_op(OpSuSt { |
| 2874 |
image_access, |
| 2875 |
image_dim: dim, |
| 2876 |
mem_order: MemOrder::Strong(MemScope::GPU), |
| 2877 |
mem_eviction_priority: self |
| 2878 |
.get_eviction_priority(intrin.access()), |
| 2879 |
handle: handle, |
| 2880 |
coord: coord, |
| 2881 |
data: data, |
| 2882 |
}); |
| 2883 |
} |
| 2884 |
nir_intrinsic_copy_fs_outputs_nv => { |
| 2885 |
let ShaderIoInfo::Fragment(info) = &mut self.info.io else { |
| 2886 |
panic!( |
| 2887 |
"copy_fs_outputs_nv is only allowed in fragment shaders" |
| 2888 |
); |
| 2889 |
}; |
| 2890 |
|
| 2891 |
for i in 0..32 { |
| 2892 |
if self.fs_out_regs[i].is_some() { |
| 2893 |
info.writes_color |= 1 << i; |
| 2894 |
} |
| 2895 |
} |
| 2896 |
let mask_idx = (NAK_FS_OUT_SAMPLE_MASK / 4) as usize; |
| 2897 |
info.writes_sample_mask = self.fs_out_regs[mask_idx].is_some(); |
| 2898 |
let depth_idx = (NAK_FS_OUT_DEPTH / 4) as usize; |
| 2899 |
info.writes_depth = self.fs_out_regs[depth_idx].is_some(); |
| 2900 |
|
| 2901 |
let mut srcs = Vec::new(); |
| 2902 |
for i in 0..8 { |
| 2903 |
// Even though the mask is per-component, the actual output |
| 2904 |
// space is per-output vec4s. |
| 2905 |
if info.writes_color & (0xf << (i * 4)) != 0 { |
| 2906 |
for c in 0..4 { |
| 2907 |
match self.fs_out_regs[i * 4 + c] { |
| 2908 |
None => srcs.push(b.undef().into()), |
| 2909 |
Some(reg) => srcs.push(reg.into()), |
| 2910 |
} |
| 2911 |
} |
| 2912 |
} |
| 2913 |
} |
| 2914 |
|
| 2915 |
// These always come together for some reason |
| 2916 |
if info.writes_sample_mask || info.writes_depth { |
| 2917 |
if info.writes_sample_mask { |
| 2918 |
srcs.push(self.fs_out_regs[mask_idx].unwrap().into()); |
| 2919 |
} else { |
| 2920 |
srcs.push(b.undef().into()); |
| 2921 |
} |
| 2922 |
if info.writes_depth { |
| 2923 |
srcs.push(self.fs_out_regs[depth_idx].unwrap().into()); |
| 2924 |
} |
| 2925 |
} |
| 2926 |
|
| 2927 |
b.push_op(OpRegOut { srcs: srcs }); |
| 2928 |
} |
| 2929 |
nir_intrinsic_demote => { |
| 2930 |
if let ShaderStageInfo::Fragment(info) = &mut self.info.stage { |
| 2931 |
info.uses_kill = true; |
| 2932 |
} else { |
| 2933 |
panic!("OpKill is only available in fragment shaders"); |
| 2934 |
} |
| 2935 |
b.push_op(OpKill {}); |
| 2936 |
} |
| 2937 |
nir_intrinsic_demote_if => { |
| 2938 |
if let ShaderStageInfo::Fragment(info) = &mut self.info.stage { |
| 2939 |
info.uses_kill = true; |
| 2940 |
} else { |
| 2941 |
panic!("OpKill is only available in fragment shaders"); |
| 2942 |
} |
| 2943 |
let cond = self.get_ssa(srcs[0].as_def())[0]; |
| 2944 |
b.predicate(cond.into()).push_op(OpKill {}); |
| 2945 |
} |
| 2946 |
nir_intrinsic_global_atomic => { |
| 2947 |
let bit_size = intrin.def.bit_size(); |
| 2948 |
let (addr, offset) = self.get_io_addr_offset(&srcs[0], 24); |
| 2949 |
let data = self.get_src(&srcs[1]); |
| 2950 |
let atom_type = self.get_atomic_type(intrin); |
| 2951 |
let atom_op = self.get_atomic_op(intrin, AtomCmpSrc::Separate); |
| 2952 |
|
| 2953 |
assert!(intrin.def.num_components() == 1); |
| 2954 |
let dst = b.alloc_ssa_vec(RegFile::GPR, bit_size.div_ceil(32)); |
| 2955 |
|
| 2956 |
let is_reduction = |
| 2957 |
atom_op.is_reduction() && intrin.def.components_read() == 0; |
| 2958 |
|
| 2959 |
b.push_op(OpAtom { |
| 2960 |
dst: if is_reduction { |
| 2961 |
Dst::None |
| 2962 |
} else { |
| 2963 |
dst.clone().into() |
| 2964 |
}, |
| 2965 |
addr: addr, |
| 2966 |
cmpr: 0.into(), |
| 2967 |
data: data, |
| 2968 |
atom_op: atom_op, |
| 2969 |
atom_type: atom_type, |
| 2970 |
addr_offset: offset, |
| 2971 |
mem_space: MemSpace::Global(MemAddrType::A64), |
| 2972 |
mem_order: MemOrder::Strong(MemScope::GPU), |
| 2973 |
mem_eviction_priority: MemEvictionPriority::Normal, // Note: no intrinic access |
| 2974 |
}); |
| 2975 |
self.set_dst(&intrin.def, dst); |
| 2976 |
} |
| 2977 |
nir_intrinsic_global_atomic_swap => { |
| 2978 |
assert!(intrin.atomic_op() == nir_atomic_op_cmpxchg); |
| 2979 |
let bit_size = intrin.def.bit_size(); |
| 2980 |
let (addr, offset) = self.get_io_addr_offset(&srcs[0], 24); |
| 2981 |
let cmpr = self.get_src(&srcs[1]); |
| 2982 |
let data = self.get_src(&srcs[2]); |
| 2983 |
let atom_type = AtomType::U(bit_size); |
| 2984 |
|
| 2985 |
assert!(intrin.def.num_components() == 1); |
| 2986 |
let dst = b.alloc_ssa_vec(RegFile::GPR, bit_size.div_ceil(32)); |
| 2987 |
|
| 2988 |
b.push_op(OpAtom { |
| 2989 |
dst: dst.clone().into(), |
| 2990 |
addr: addr, |
| 2991 |
cmpr: cmpr, |
| 2992 |
data: data, |
| 2993 |
atom_op: AtomOp::CmpExch(AtomCmpSrc::Separate), |
| 2994 |
atom_type: atom_type, |
| 2995 |
addr_offset: offset, |
| 2996 |
mem_space: MemSpace::Global(MemAddrType::A64), |
| 2997 |
mem_order: MemOrder::Strong(MemScope::GPU), |
| 2998 |
mem_eviction_priority: MemEvictionPriority::Normal, // Note: no intrinic access |
| 2999 |
}); |
| 3000 |
self.set_dst(&intrin.def, dst); |
| 3001 |
} |
| 3002 |
nir_intrinsic_ipa_nv => { |
| 3003 |
let addr = u16::try_from(intrin.base()).unwrap(); |
| 3004 |
|
| 3005 |
let flags = intrin.flags(); |
| 3006 |
let flags: nak_nir_ipa_flags = |
| 3007 |
unsafe { std::mem::transmute_copy(&flags) }; |
| 3008 |
|
| 3009 |
let mode = match flags.interp_mode() { |
| 3010 |
NAK_INTERP_MODE_PERSPECTIVE => PixelImap::Perspective, |
| 3011 |
NAK_INTERP_MODE_SCREEN_LINEAR => PixelImap::ScreenLinear, |
| 3012 |
NAK_INTERP_MODE_CONSTANT => PixelImap::Constant, |
| 3013 |
_ => panic!("Unsupported interp mode"), |
| 3014 |
}; |
| 3015 |
|
| 3016 |
let freq = match flags.interp_freq() { |
| 3017 |
NAK_INTERP_FREQ_PASS => InterpFreq::Pass, |
| 3018 |
NAK_INTERP_FREQ_PASS_MUL_W => InterpFreq::PassMulW, |
| 3019 |
NAK_INTERP_FREQ_CONSTANT => InterpFreq::Constant, |
| 3020 |
NAK_INTERP_FREQ_STATE => InterpFreq::State, |
| 3021 |
_ => panic!("Invalid interp freq"), |
| 3022 |
}; |
| 3023 |
|
| 3024 |
let loc = match flags.interp_loc() { |
| 3025 |
NAK_INTERP_LOC_DEFAULT => InterpLoc::Default, |
| 3026 |
NAK_INTERP_LOC_CENTROID => InterpLoc::Centroid, |
| 3027 |
NAK_INTERP_LOC_OFFSET => InterpLoc::Offset, |
| 3028 |
_ => panic!("Invalid interp loc"), |
| 3029 |
}; |
| 3030 |
|
| 3031 |
let inv_w = if freq == InterpFreq::PassMulW { |
| 3032 |
self.get_src(&srcs[0]) |
| 3033 |
} else { |
| 3034 |
0.into() |
| 3035 |
}; |
| 3036 |
|
| 3037 |
let offset = if loc == InterpLoc::Offset { |
| 3038 |
self.get_src(&srcs[1]) |
| 3039 |
} else { |
| 3040 |
0.into() |
| 3041 |
}; |
| 3042 |
|
| 3043 |
let ShaderIoInfo::Fragment(io) = &mut self.info.io else { |
| 3044 |
panic!("OpIpa is only used for fragment shaders"); |
| 3045 |
}; |
| 3046 |
|
| 3047 |
io.mark_attr_read(addr, mode); |
| 3048 |
|
| 3049 |
let dst = b.alloc_ssa(RegFile::GPR); |
| 3050 |
b.push_op(OpIpa { |
| 3051 |
dst: dst.into(), |
| 3052 |
addr: addr, |
| 3053 |
freq: freq, |
| 3054 |
loc: loc, |
| 3055 |
inv_w: inv_w, |
| 3056 |
offset: offset, |
| 3057 |
}); |
| 3058 |
self.set_dst(&intrin.def, dst.into()); |
| 3059 |
} |
| 3060 |
nir_intrinsic_isberd_nv => { |
| 3061 |
let dst = b.alloc_ssa(RegFile::GPR); |
| 3062 |
b.push_op(OpIsberd { |
| 3063 |
dst: dst.into(), |
| 3064 |
idx: self.get_src(&srcs[0]), |
| 3065 |
}); |
| 3066 |
self.set_dst(&intrin.def, dst.into()); |
| 3067 |
} |
| 3068 |
nir_intrinsic_vild_nv => { |
| 3069 |
let dst = b.alloc_ssa(RegFile::GPR); |
| 3070 |
|
| 3071 |
let (idx, off) = self.get_io_addr_offset(&srcs[0], 8); |
| 3072 |
b.push_op(OpViLd { |
| 3073 |
dst: dst.into(), |
| 3074 |
idx, |
| 3075 |
off: off.try_into().unwrap(), |
| 3076 |
}); |
| 3077 |
self.set_dst(&intrin.def, dst.into()); |
| 3078 |
} |
| 3079 |
nir_intrinsic_load_barycentric_at_offset_nv => (), |
| 3080 |
nir_intrinsic_load_barycentric_centroid => (), |
| 3081 |
nir_intrinsic_load_barycentric_pixel => (), |
| 3082 |
nir_intrinsic_load_barycentric_sample => (), |
| 3083 |
nir_intrinsic_load_global | nir_intrinsic_load_global_constant => { |
| 3084 |
let size_B = |
| 3085 |
(intrin.def.bit_size() / 8) * intrin.def.num_components(); |
| 3086 |
assert!(u32::from(size_B) <= intrin.align()); |
| 3087 |
let order = if intrin.intrinsic |
| 3088 |
== nir_intrinsic_load_global_constant |
| 3089 |
|| (intrin.access() & ACCESS_CAN_REORDER) != 0 |
| 3090 |
{ |
| 3091 |
MemOrder::Constant |
| 3092 |
} else { |
| 3093 |
MemOrder::Strong(MemScope::GPU) |
| 3094 |
}; |
| 3095 |
let access = MemAccess { |
| 3096 |
mem_type: MemType::from_size(size_B, false), |
| 3097 |
space: MemSpace::Global(MemAddrType::A64), |
| 3098 |
order: order, |
| 3099 |
eviction_priority: self |
| 3100 |
.get_eviction_priority(intrin.access()), |
| 3101 |
}; |
| 3102 |
let (addr, offset) = self.get_io_addr_offset(&srcs[0], 24); |
| 3103 |
let dst = b.alloc_ssa_vec(RegFile::GPR, size_B.div_ceil(4)); |
| 3104 |
|
| 3105 |
b.push_op(OpLd { |
| 3106 |
dst: dst.clone().into(), |
| 3107 |
addr: addr, |
| 3108 |
offset: offset, |
| 3109 |
access: access, |
| 3110 |
}); |
| 3111 |
self.set_dst(&intrin.def, dst); |
| 3112 |
} |
| 3113 |
nir_intrinsic_ldtram_nv => { |
| 3114 |
let ShaderIoInfo::Fragment(io) = &mut self.info.io else { |
| 3115 |
panic!("ldtram_nv is only used for fragment shaders"); |
| 3116 |
}; |
| 3117 |
|
| 3118 |
assert!( |
| 3119 |
intrin.def.bit_size() == 32 |
| 3120 |
&& intrin.def.num_components == 2 |
| 3121 |
); |
| 3122 |
|
| 3123 |
let flags = intrin.flags(); |
| 3124 |
let use_c = flags != 0; |
| 3125 |
|
| 3126 |
let addr = u16::try_from(intrin.base()).unwrap(); |
| 3127 |
|
| 3128 |
io.mark_barycentric_attr_in(addr); |
| 3129 |
|
| 3130 |
let dst = b.alloc_ssa_vec(RegFile::GPR, 2); |
| 3131 |
b.push_op(OpLdTram { |
| 3132 |
dst: dst.clone().into(), |
| 3133 |
addr, |
| 3134 |
use_c, |
| 3135 |
}); |
| 3136 |
self.set_dst(&intrin.def, dst); |
| 3137 |
} |
| 3138 |
nir_intrinsic_load_sample_id => { |
| 3139 |
let dst = b.alloc_ssa(RegFile::GPR); |
| 3140 |
b.push_op(OpPixLd { |
| 3141 |
dst: dst.into(), |
| 3142 |
val: PixVal::MyIndex, |
| 3143 |
}); |
| 3144 |
self.set_dst(&intrin.def, dst.into()); |
| 3145 |
} |
| 3146 |
nir_intrinsic_load_sample_mask_in => { |
| 3147 |
if let ShaderIoInfo::Fragment(info) = &mut self.info.io { |
| 3148 |
info.reads_sample_mask = true; |
| 3149 |
} else { |
| 3150 |
panic!( |
| 3151 |
"sample_mask_in is only available in fragment shaders" |
| 3152 |
); |
| 3153 |
} |
| 3154 |
|
| 3155 |
let dst = b.alloc_ssa(RegFile::GPR); |
| 3156 |
b.push_op(OpPixLd { |
| 3157 |
dst: dst.into(), |
| 3158 |
val: PixVal::CovMask, |
| 3159 |
}); |
| 3160 |
self.set_dst(&intrin.def, dst.into()); |
| 3161 |
} |
| 3162 |
nir_intrinsic_load_tess_coord_xy => { |
| 3163 |
// Loading gl_TessCoord in tessellation evaluation shaders is |
| 3164 |
// weird. It's treated as a per-vertex output which is indexed |
| 3165 |
// by LANEID. |
| 3166 |
match &self.info.stage { |
| 3167 |
ShaderStageInfo::Tessellation(_) => (), |
| 3168 |
_ => panic!( |
| 3169 |
"load_tess_coord is only available in tessellation \ |
| 3170 |
shaders" |
| 3171 |
), |
| 3172 |
}; |
| 3173 |
|
| 3174 |
assert!(intrin.def.bit_size() == 32); |
| 3175 |
assert!(intrin.def.num_components() == 2); |
| 3176 |
|
| 3177 |
let vtx = b.alloc_ssa(RegFile::GPR); |
| 3178 |
b.push_op(OpS2R { |
| 3179 |
dst: vtx.into(), |
| 3180 |
idx: 0, |
| 3181 |
}); |
| 3182 |
|
| 3183 |
// This is recorded as a patch output in parse_shader() because |
| 3184 |
// the hardware requires it be in the SPH, whether we use it or |
| 3185 |
// not. |
| 3186 |
|
| 3187 |
let comps = 2; |
| 3188 |
let dst = b.alloc_ssa_vec(RegFile::GPR, comps); |
| 3189 |
b.push_op(OpALd { |
| 3190 |
dst: dst.clone().into(), |
| 3191 |
vtx: vtx.into(), |
| 3192 |
addr: NAK_ATTR_TESS_COORD, |
| 3193 |
offset: 0.into(), |
| 3194 |
comps, |
| 3195 |
patch: false, |
| 3196 |
output: true, |
| 3197 |
phys: false, |
| 3198 |
}); |
| 3199 |
self.set_dst(&intrin.def, dst); |
| 3200 |
} |
| 3201 |
nir_intrinsic_load_scratch => { |
| 3202 |
let size_B = |
| 3203 |
(intrin.def.bit_size() / 8) * intrin.def.num_components(); |
| 3204 |
assert!(u32::from(size_B) <= intrin.align()); |
| 3205 |
let access = MemAccess { |
| 3206 |
mem_type: MemType::from_size(size_B, false), |
| 3207 |
space: MemSpace::Local, |
| 3208 |
order: MemOrder::Strong(MemScope::CTA), |
| 3209 |
eviction_priority: MemEvictionPriority::Normal, |
| 3210 |
}; |
| 3211 |
let (addr, offset) = self.get_io_addr_offset(&srcs[0], 24); |
| 3212 |
let dst = b.alloc_ssa_vec(RegFile::GPR, size_B.div_ceil(4)); |
| 3213 |
|
| 3214 |
b.push_op(OpLd { |
| 3215 |
dst: dst.clone().into(), |
| 3216 |
addr: addr, |
| 3217 |
offset: offset, |
| 3218 |
access: access, |
| 3219 |
}); |
| 3220 |
self.set_dst(&intrin.def, dst); |
| 3221 |
} |
| 3222 |
nir_intrinsic_load_shared => { |
| 3223 |
let size_B = |
| 3224 |
(intrin.def.bit_size() / 8) * intrin.def.num_components(); |
| 3225 |
assert!(u32::from(size_B) <= intrin.align()); |
| 3226 |
let access = MemAccess { |
| 3227 |
mem_type: MemType::from_size(size_B, false), |
| 3228 |
space: MemSpace::Shared, |
| 3229 |
order: MemOrder::Strong(MemScope::CTA), |
| 3230 |
eviction_priority: MemEvictionPriority::Normal, |
| 3231 |
}; |
| 3232 |
let (addr, offset) = self.get_io_addr_offset(&srcs[0], 24); |
| 3233 |
let offset = offset + intrin.base(); |
| 3234 |
let dst = b.alloc_ssa_vec(RegFile::GPR, size_B.div_ceil(4)); |
| 3235 |
|
| 3236 |
b.push_op(OpLd { |
| 3237 |
dst: dst.clone().into(), |
| 3238 |
addr: addr, |
| 3239 |
offset: offset, |
| 3240 |
access: access, |
| 3241 |
}); |
| 3242 |
self.set_dst(&intrin.def, dst); |
| 3243 |
} |
| 3244 |
nir_intrinsic_load_shared_lock_nv => { |
| 3245 |
let size_B = intrin.def.bit_size() / 8; |
| 3246 |
let mem_type = MemType::from_size(size_B, false); |
| 3247 |
|
| 3248 |
let (addr, offset) = self.get_io_addr_offset(&srcs[0], 24); |
| 3249 |
let dst = b.alloc_ssa_vec(RegFile::GPR, size_B.div_ceil(4)); |
| 3250 |
let locked = b.alloc_ssa(RegFile::Pred); |
| 3251 |
|
| 3252 |
b.push_op(OpLdSharedLock { |
| 3253 |
dst: dst.clone().into(), |
| 3254 |
locked: locked.into(), |
| 3255 |
addr, |
| 3256 |
offset, |
| 3257 |
mem_type, |
| 3258 |
}); |
| 3259 |
let locked_gpr = b.sel(locked.into(), 1.into(), 0.into()); |
| 3260 |
|
| 3261 |
// for 32-bit we have 2x32 return type, |
| 3262 |
// for 64-bit we need 2x64, so is_locked must be a 64-bit val. |
| 3263 |
// we can fill the remaining SSAValue with a copy of is_locked |
| 3264 |
let locked_dst = std::iter::repeat_n(locked_gpr, dst.len()); |
| 3265 |
let nir_dst: Vec<_> = |
| 3266 |
dst.iter().copied().chain(locked_dst).collect(); |
| 3267 |
self.set_ssa(intrin.def.as_def(), nir_dst); |
| 3268 |
} |
| 3269 |
nir_intrinsic_load_sysval_nv => { |
| 3270 |
let idx = u8::try_from(intrin.base()).unwrap(); |
| 3271 |
debug_assert!(intrin.def.num_components == 1); |
| 3272 |
debug_assert!( |
| 3273 |
intrin.def.bit_size == 32 || intrin.def.bit_size == 64 |
| 3274 |
); |
| 3275 |
let comps = intrin.def.bit_size / 32; |
| 3276 |
let dst = b.alloc_ssa_vec(RegFile::GPR, comps); |
| 3277 |
if self.sm.sm() >= 50 |
| 3278 |
&& (idx == NAK_SV_CLOCK || idx == NAK_SV_CLOCK + 1) |
| 3279 |
{ |
| 3280 |
debug_assert!(idx + comps <= NAK_SV_CLOCK + 2); |
| 3281 |
b.push_op(OpCS2R { |
| 3282 |
dst: dst.clone().into(), |
| 3283 |
idx: idx, |
| 3284 |
}); |
| 3285 |
} else { |
| 3286 |
debug_assert!(intrin.def.bit_size == 32); |
| 3287 |
b.push_op(OpS2R { |
| 3288 |
dst: dst.clone().into(), |
| 3289 |
idx: idx, |
| 3290 |
}); |
| 3291 |
} |
| 3292 |
self.set_dst(&intrin.def, dst); |
| 3293 |
} |
| 3294 |
nir_intrinsic_ldc_nv => { |
| 3295 |
let size_B = |
| 3296 |
(intrin.def.bit_size() / 8) * intrin.def.num_components(); |
| 3297 |
let idx = &srcs[0]; |
| 3298 |
|
| 3299 |
let (off, off_imm) = self.get_cbuf_addr_offset(&srcs[1]); |
| 3300 |
|
| 3301 |
let dst = b.alloc_ssa_vec(RegFile::GPR, size_B.div_ceil(4)); |
| 3302 |
|
| 3303 |
if let Some(idx_imm) = idx.as_uint() { |
| 3304 |
let idx_imm: u8 = idx_imm.try_into().unwrap(); |
| 3305 |
let cb = CBufRef { |
| 3306 |
buf: CBuf::Binding(idx_imm), |
| 3307 |
offset: off_imm, |
| 3308 |
}; |
| 3309 |
if off.is_zero() { |
| 3310 |
for (i, comp) in dst.iter().enumerate() { |
| 3311 |
let i = u16::try_from(i).unwrap(); |
| 3312 |
b.copy_to( |
| 3313 |
(*comp).into(), |
| 3314 |
cb.clone().offset(i * 4).into(), |
| 3315 |
); |
| 3316 |
} |
| 3317 |
} else { |
| 3318 |
b.push_op(OpLdc { |
| 3319 |
dst: dst.clone().into(), |
| 3320 |
cb: cb.into(), |
| 3321 |
offset: off, |
| 3322 |
mode: LdcMode::Indexed, |
| 3323 |
mem_type: MemType::from_size(size_B, false), |
| 3324 |
}); |
| 3325 |
} |
| 3326 |
} else { |
| 3327 |
// In the IndexedSegmented mode, the hardware computes the |
| 3328 |
// actual index and offset as follows: |
| 3329 |
// |
| 3330 |
// idx = imm_idx + reg[31:16] |
| 3331 |
// offset = imm_offset + reg[15:0] |
| 3332 |
// ldc c[idx][offset] |
| 3333 |
// |
| 3334 |
// So pack the index and offset accordingly |
| 3335 |
let idx = self.get_src(idx); |
| 3336 |
let off_idx = b.prmt(off, idx, [0, 1, 4, 5]); |
| 3337 |
let cb = CBufRef { |
| 3338 |
buf: CBuf::Binding(0), |
| 3339 |
offset: off_imm, |
| 3340 |
}; |
| 3341 |
b.push_op(OpLdc { |
| 3342 |
dst: dst.clone().into(), |
| 3343 |
cb: cb.into(), |
| 3344 |
offset: off_idx.into(), |
| 3345 |
mode: LdcMode::IndexedSegmented, |
| 3346 |
mem_type: MemType::from_size(size_B, false), |
| 3347 |
}); |
| 3348 |
} |
| 3349 |
self.set_dst(&intrin.def, dst); |
| 3350 |
} |
| 3351 |
nir_intrinsic_ldcx_nv => { |
| 3352 |
let size_B = |
| 3353 |
(intrin.def.bit_size() / 8) * intrin.def.num_components(); |
| 3354 |
|
| 3355 |
let handle = self.get_ssa_ref(&srcs[0]); |
| 3356 |
let (off, off_imm) = self.get_cbuf_addr_offset(&srcs[1]); |
| 3357 |
|
| 3358 |
let cb = CBufRef { |
| 3359 |
buf: CBuf::BindlessSSA(handle[..].try_into().unwrap()), |
| 3360 |
offset: off_imm, |
| 3361 |
}; |
| 3362 |
|
| 3363 |
let dst = b.alloc_ssa_vec(RegFile::GPR, size_B.div_ceil(4)); |
| 3364 |
if off.is_zero() { |
| 3365 |
for (i, comp) in dst.iter().enumerate() { |
| 3366 |
let i = u16::try_from(i).unwrap(); |
| 3367 |
b.copy_to( |
| 3368 |
(*comp).into(), |
| 3369 |
cb.clone().offset(i * 4).into(), |
| 3370 |
); |
| 3371 |
} |
| 3372 |
} else { |
| 3373 |
b.push_op(OpLdc { |
| 3374 |
dst: dst.clone().into(), |
| 3375 |
cb: cb.into(), |
| 3376 |
offset: off, |
| 3377 |
mode: LdcMode::Indexed, |
| 3378 |
mem_type: MemType::from_size(size_B, false), |
| 3379 |
}); |
| 3380 |
} |
| 3381 |
self.set_dst(&intrin.def, dst); |
| 3382 |
} |
| 3383 |
nir_intrinsic_pin_cx_handle_nv => { |
| 3384 |
let handle = self.get_ssa_ref(&srcs[0]); |
| 3385 |
b.push_op(OpPin { |
| 3386 |
src: handle.clone().into(), |
| 3387 |
dst: handle.into(), |
| 3388 |
}); |
| 3389 |
} |
| 3390 |
nir_intrinsic_unpin_cx_handle_nv => { |
| 3391 |
let handle = self.get_ssa_ref(&srcs[0]); |
| 3392 |
b.push_op(OpUnpin { |
| 3393 |
src: handle.clone().into(), |
| 3394 |
dst: handle.into(), |
| 3395 |
}); |
| 3396 |
} |
| 3397 |
nir_intrinsic_barrier => { |
| 3398 |
let modes = intrin.memory_modes(); |
| 3399 |
let semantics = intrin.memory_semantics(); |
| 3400 |
if (modes & nir_var_mem_global) != 0 |
| 3401 |
&& (semantics & NIR_MEMORY_RELEASE) != 0 |
| 3402 |
{ |
| 3403 |
// Pre-Volta doesn't have WBAll but it also seems that we |
| 3404 |
// don't need it. |
| 3405 |
if self.sm.sm() >= 70 { |
| 3406 |
b.push_op(OpCCtl { |
| 3407 |
op: CCtlOp::WBAll, |
| 3408 |
mem_space: MemSpace::Global(MemAddrType::A64), |
| 3409 |
addr: 0.into(), |
| 3410 |
addr_offset: 0, |
| 3411 |
}); |
| 3412 |
} |
| 3413 |
} |
| 3414 |
match intrin.execution_scope() { |
| 3415 |
SCOPE_NONE => (), |
| 3416 |
SCOPE_WORKGROUP => { |
| 3417 |
assert!( |
| 3418 |
self.nir.info.stage() == MESA_SHADER_COMPUTE |
| 3419 |
|| self.nir.info.stage() == MESA_SHADER_KERNEL |
| 3420 |
); |
| 3421 |
self.info.num_control_barriers = 1; |
| 3422 |
b.push_op(OpBar {}); |
| 3423 |
} |
| 3424 |
_ => panic!("Unhandled execution scope"), |
| 3425 |
} |
| 3426 |
if intrin.memory_scope() != SCOPE_NONE { |
| 3427 |
let mem_scope = match intrin.memory_scope() { |
| 3428 |
SCOPE_INVOCATION | SCOPE_SUBGROUP => MemScope::CTA, |
| 3429 |
// A membar.gpu is very expensive so use .cta whenever |
| 3430 |
// possible. |
| 3431 |
// TODO: Figure out under which conditions we can relax |
| 3432 |
// them for global memory/images to CTA. |
| 3433 |
SCOPE_WORKGROUP => { |
| 3434 |
let global_modes = nir_var_image |
| 3435 |
| nir_var_mem_global |
| 3436 |
| nir_var_mem_ssbo; |
| 3437 |
if intrin.memory_modes() & global_modes != 0 { |
| 3438 |
MemScope::GPU |
| 3439 |
} else { |
| 3440 |
MemScope::CTA |
| 3441 |
} |
| 3442 |
} |
| 3443 |
SCOPE_QUEUE_FAMILY | SCOPE_DEVICE => MemScope::GPU, |
| 3444 |
_ => panic!("Unhandled memory scope"), |
| 3445 |
}; |
| 3446 |
b.push_op(OpMemBar { scope: mem_scope }); |
| 3447 |
} |
| 3448 |
if (modes & nir_var_mem_global) != 0 |
| 3449 |
&& (semantics & NIR_MEMORY_ACQUIRE) != 0 |
| 3450 |
{ |
| 3451 |
b.push_op(OpCCtl { |
| 3452 |
op: CCtlOp::IVAll, |
| 3453 |
mem_space: MemSpace::Global(MemAddrType::A64), |
| 3454 |
addr: 0.into(), |
| 3455 |
addr_offset: 0, |
| 3456 |
}); |
| 3457 |
} |
| 3458 |
} |
| 3459 |
nir_intrinsic_quad_broadcast |
| 3460 |
| nir_intrinsic_read_invocation |
| 3461 |
| nir_intrinsic_shuffle |
| 3462 |
| nir_intrinsic_shuffle_down |
| 3463 |
| nir_intrinsic_shuffle_up |
| 3464 |
| nir_intrinsic_shuffle_xor => { |
| 3465 |
assert!(srcs[0].bit_size() == 32); |
| 3466 |
assert!(srcs[0].num_components() == 1); |
| 3467 |
let data = self.get_src(&srcs[0]); |
| 3468 |
|
| 3469 |
assert!(srcs[1].bit_size() == 32); |
| 3470 |
let idx = self.get_src(&srcs[1]); |
| 3471 |
|
| 3472 |
assert!(intrin.def.bit_size() == 32); |
| 3473 |
let dst = b.alloc_ssa(RegFile::GPR); |
| 3474 |
|
| 3475 |
b.push_op(OpShfl { |
| 3476 |
dst: dst.into(), |
| 3477 |
in_bounds: Dst::None, |
| 3478 |
src: data, |
| 3479 |
lane: idx, |
| 3480 |
c: match intrin.intrinsic { |
| 3481 |
nir_intrinsic_quad_broadcast => 0x1c_03.into(), |
| 3482 |
nir_intrinsic_shuffle_up => 0.into(), |
| 3483 |
_ => 0x1f.into(), |
| 3484 |
}, |
| 3485 |
op: match intrin.intrinsic { |
| 3486 |
nir_intrinsic_shuffle_down => ShflOp::Down, |
| 3487 |
nir_intrinsic_shuffle_up => ShflOp::Up, |
| 3488 |
nir_intrinsic_shuffle_xor => ShflOp::Bfly, |
| 3489 |
_ => ShflOp::Idx, |
| 3490 |
}, |
| 3491 |
}); |
| 3492 |
self.set_dst(&intrin.def, dst.into()); |
| 3493 |
} |
| 3494 |
nir_intrinsic_quad_swap_horizontal |
| 3495 |
| nir_intrinsic_quad_swap_vertical |
| 3496 |
| nir_intrinsic_quad_swap_diagonal => { |
| 3497 |
assert!(srcs[0].bit_size() == 32); |
| 3498 |
assert!(srcs[0].num_components() == 1); |
| 3499 |
let data = self.get_src(&srcs[0]); |
| 3500 |
|
| 3501 |
assert!(intrin.def.bit_size() == 32); |
| 3502 |
let dst = b.alloc_ssa(RegFile::GPR); |
| 3503 |
b.push_op(OpShfl { |
| 3504 |
dst: dst.into(), |
| 3505 |
in_bounds: Dst::None, |
| 3506 |
src: data, |
| 3507 |
lane: match intrin.intrinsic { |
| 3508 |
nir_intrinsic_quad_swap_horizontal => 1_u32.into(), |
| 3509 |
nir_intrinsic_quad_swap_vertical => 2_u32.into(), |
| 3510 |
nir_intrinsic_quad_swap_diagonal => 3_u32.into(), |
| 3511 |
op => panic!("Unknown quad intrinsic {}", op), |
| 3512 |
}, |
| 3513 |
c: 0x1c_03.into(), |
| 3514 |
op: ShflOp::Bfly, |
| 3515 |
}); |
| 3516 |
self.set_dst(&intrin.def, dst.into()); |
| 3517 |
} |
| 3518 |
nir_intrinsic_reduce => { |
| 3519 |
assert!(srcs[0].bit_size() == 32); |
| 3520 |
assert!(srcs[0].num_components() == 1); |
| 3521 |
let src = self.get_src(&srcs[0]); |
| 3522 |
let dst = b.alloc_ssa(RegFile::UGPR); |
| 3523 |
|
| 3524 |
let op = match intrin.reduction_op() { |
| 3525 |
nir_op_iand => ReduxOp::And, |
| 3526 |
nir_op_ior => ReduxOp::Or, |
| 3527 |
nir_op_ixor => ReduxOp::Xor, |
| 3528 |
nir_op_iadd => ReduxOp::Sum, |
| 3529 |
nir_op_imin => ReduxOp::Min(IntCmpType::I32), |
| 3530 |
nir_op_imax => ReduxOp::Max(IntCmpType::I32), |
| 3531 |
nir_op_umin => ReduxOp::Min(IntCmpType::U32), |
| 3532 |
nir_op_umax => ReduxOp::Max(IntCmpType::U32), |
| 3533 |
_ => panic!("Unknown reduction op"), |
| 3534 |
}; |
| 3535 |
|
| 3536 |
b.push_op(OpRedux { |
| 3537 |
dst: dst.into(), |
| 3538 |
src: src, |
| 3539 |
op, |
| 3540 |
}); |
| 3541 |
self.set_dst(&intrin.def, dst.into()); |
| 3542 |
} |
| 3543 |
nir_intrinsic_shared_atomic => { |
| 3544 |
let bit_size = intrin.def.bit_size(); |
| 3545 |
let (addr, offset) = self.get_io_addr_offset(&srcs[0], 24); |
| 3546 |
let data = self.get_src(&srcs[1]); |
| 3547 |
let atom_type = self.get_atomic_type(intrin); |
| 3548 |
let atom_op = self.get_atomic_op(intrin, AtomCmpSrc::Separate); |
| 3549 |
|
| 3550 |
assert!(intrin.def.num_components() == 1); |
| 3551 |
let dst = b.alloc_ssa_vec(RegFile::GPR, bit_size.div_ceil(32)); |
| 3552 |
|
| 3553 |
b.push_op(OpAtom { |
| 3554 |
dst: dst.clone().into(), |
| 3555 |
addr: addr, |
| 3556 |
cmpr: 0.into(), |
| 3557 |
data: data, |
| 3558 |
atom_op: atom_op, |
| 3559 |
atom_type: atom_type, |
| 3560 |
addr_offset: offset, |
| 3561 |
mem_space: MemSpace::Shared, |
| 3562 |
mem_order: MemOrder::Strong(MemScope::CTA), |
| 3563 |
mem_eviction_priority: MemEvictionPriority::Normal, |
| 3564 |
}); |
| 3565 |
self.set_dst(&intrin.def, dst); |
| 3566 |
} |
| 3567 |
nir_intrinsic_shared_atomic_swap => { |
| 3568 |
assert!(intrin.atomic_op() == nir_atomic_op_cmpxchg); |
| 3569 |
let bit_size = intrin.def.bit_size(); |
| 3570 |
let (addr, offset) = self.get_io_addr_offset(&srcs[0], 24); |
| 3571 |
let cmpr = self.get_src(&srcs[1]); |
| 3572 |
let data = self.get_src(&srcs[2]); |
| 3573 |
let atom_type = AtomType::U(bit_size); |
| 3574 |
|
| 3575 |
assert!(intrin.def.num_components() == 1); |
| 3576 |
let dst = b.alloc_ssa_vec(RegFile::GPR, bit_size.div_ceil(32)); |
| 3577 |
|
| 3578 |
b.push_op(OpAtom { |
| 3579 |
dst: dst.clone().into(), |
| 3580 |
addr: addr, |
| 3581 |
cmpr: cmpr, |
| 3582 |
data: data, |
| 3583 |
atom_op: AtomOp::CmpExch(AtomCmpSrc::Separate), |
| 3584 |
atom_type: atom_type, |
| 3585 |
addr_offset: offset, |
| 3586 |
mem_space: MemSpace::Shared, |
| 3587 |
mem_order: MemOrder::Strong(MemScope::CTA), |
| 3588 |
mem_eviction_priority: MemEvictionPriority::Normal, |
| 3589 |
}); |
| 3590 |
self.set_dst(&intrin.def, dst); |
| 3591 |
} |
| 3592 |
nir_intrinsic_ssa_bar_nv => { |
| 3593 |
let src = self.get_src(&srcs[0]); |
| 3594 |
b.push_op(OpSrcBar { src }); |
| 3595 |
} |
| 3596 |
nir_intrinsic_store_global => { |
| 3597 |
let data = self.get_src(&srcs[0]); |
| 3598 |
let size_B = |
| 3599 |
(srcs[0].bit_size() / 8) * srcs[0].num_components(); |
| 3600 |
assert!(u32::from(size_B) <= intrin.align()); |
| 3601 |
let access = MemAccess { |
| 3602 |
mem_type: MemType::from_size(size_B, false), |
| 3603 |
space: MemSpace::Global(MemAddrType::A64), |
| 3604 |
order: MemOrder::Strong(MemScope::GPU), |
| 3605 |
eviction_priority: self |
| 3606 |
.get_eviction_priority(intrin.access()), |
| 3607 |
}; |
| 3608 |
let (addr, offset) = self.get_io_addr_offset(&srcs[1], 24); |
| 3609 |
|
| 3610 |
b.push_op(OpSt { |
| 3611 |
addr: addr, |
| 3612 |
data: data, |
| 3613 |
offset: offset, |
| 3614 |
access: access, |
| 3615 |
}); |
| 3616 |
} |
| 3617 |
nir_intrinsic_fs_out_nv => { |
| 3618 |
let data = self.get_ssa(srcs[0].as_def()); |
| 3619 |
assert!(data.len() == 1); |
| 3620 |
let data = data[0]; |
| 3621 |
|
| 3622 |
let addr = u16::try_from(intrin.base()).unwrap(); |
| 3623 |
assert!(addr % 4 == 0); |
| 3624 |
|
| 3625 |
self.fs_out_regs[usize::from(addr / 4)] = Some(data); |
| 3626 |
} |
| 3627 |
nir_intrinsic_store_scratch => { |
| 3628 |
let data = self.get_src(&srcs[0]); |
| 3629 |
let size_B = |
| 3630 |
(srcs[0].bit_size() / 8) * srcs[0].num_components(); |
| 3631 |
assert!(u32::from(size_B) <= intrin.align()); |
| 3632 |
let access = MemAccess { |
| 3633 |
mem_type: MemType::from_size(size_B, false), |
| 3634 |
space: MemSpace::Local, |
| 3635 |
order: MemOrder::Strong(MemScope::CTA), |
| 3636 |
eviction_priority: MemEvictionPriority::Normal, |
| 3637 |
}; |
| 3638 |
let (addr, offset) = self.get_io_addr_offset(&srcs[1], 24); |
| 3639 |
|
| 3640 |
b.push_op(OpSt { |
| 3641 |
addr: addr, |
| 3642 |
data: data, |
| 3643 |
offset: offset, |
| 3644 |
access: access, |
| 3645 |
}); |
| 3646 |
} |
| 3647 |
nir_intrinsic_store_shared => { |
| 3648 |
let data = self.get_src(&srcs[0]); |
| 3649 |
let size_B = |
| 3650 |
(srcs[0].bit_size() / 8) * srcs[0].num_components(); |
| 3651 |
assert!(u32::from(size_B) <= intrin.align()); |
| 3652 |
let access = MemAccess { |
| 3653 |
mem_type: MemType::from_size(size_B, false), |
| 3654 |
space: MemSpace::Shared, |
| 3655 |
order: MemOrder::Strong(MemScope::CTA), |
| 3656 |
eviction_priority: MemEvictionPriority::Normal, |
| 3657 |
}; |
| 3658 |
let (addr, offset) = self.get_io_addr_offset(&srcs[1], 24); |
| 3659 |
let offset = offset + intrin.base(); |
| 3660 |
|
| 3661 |
b.push_op(OpSt { |
| 3662 |
addr: addr, |
| 3663 |
data: data, |
| 3664 |
offset: offset, |
| 3665 |
access: access, |
| 3666 |
}); |
| 3667 |
} |
| 3668 |
nir_intrinsic_store_shared_unlock_nv => { |
| 3669 |
let data = self.get_src(&srcs[0]); |
| 3670 |
let size_B = |
| 3671 |
(srcs[0].bit_size() / 8) * srcs[0].num_components(); |
| 3672 |
let mem_type = MemType::from_size(size_B, false); |
| 3673 |
|
| 3674 |
let (addr, offset) = self.get_io_addr_offset(&srcs[1], 24); |
| 3675 |
let locked = b.alloc_ssa(RegFile::Pred); |
| 3676 |
|
| 3677 |
b.push_op(OpStSCheckUnlock { |
| 3678 |
locked: locked.into(), |
| 3679 |
addr, |
| 3680 |
data, |
| 3681 |
offset, |
| 3682 |
mem_type, |
| 3683 |
}); |
| 3684 |
let locked_gpr = b.sel(locked.into(), 1.into(), 0.into()); |
| 3685 |
self.set_dst(intrin.def.as_def(), locked_gpr.into()); |
| 3686 |
} |
| 3687 |
nir_intrinsic_emit_vertex_nv | nir_intrinsic_end_primitive_nv => { |
| 3688 |
assert!(intrin.def.bit_size() == 32); |
| 3689 |
assert!(intrin.def.num_components() == 1); |
| 3690 |
|
| 3691 |
let dst = b.alloc_ssa(RegFile::GPR); |
| 3692 |
let handle = self.get_src(&srcs[0]); |
| 3693 |
let stream_id = intrin.stream_id(); |
| 3694 |
|
| 3695 |
b.push_op(OpOut { |
| 3696 |
dst: dst.into(), |
| 3697 |
handle: handle, |
| 3698 |
stream: stream_id.into(), |
| 3699 |
out_type: if intrin.intrinsic |
| 3700 |
== nir_intrinsic_emit_vertex_nv |
| 3701 |
{ |
| 3702 |
OutType::Emit |
| 3703 |
} else { |
| 3704 |
OutType::Cut |
| 3705 |
}, |
| 3706 |
}); |
| 3707 |
self.set_dst(&intrin.def, dst.into()); |
| 3708 |
} |
| 3709 |
|
| 3710 |
nir_intrinsic_final_primitive_nv => { |
| 3711 |
let handle = self.get_src(&srcs[0]); |
| 3712 |
|
| 3713 |
if self.sm.sm() >= 70 { |
| 3714 |
b.push_op(OpOutFinal { handle: handle }); |
| 3715 |
} else { |
| 3716 |
b.push_op(OpRegOut { srcs: vec![handle] }); |
| 3717 |
} |
| 3718 |
} |
| 3719 |
nir_intrinsic_vote_all |
| 3720 |
| nir_intrinsic_vote_any |
| 3721 |
| nir_intrinsic_vote_ieq => { |
| 3722 |
let src = self.get_src(&srcs[0]); |
| 3723 |
let src_bits = srcs[0].bit_size() * srcs[0].num_components(); |
| 3724 |
|
| 3725 |
assert!(intrin.def.bit_size() == 1); |
| 3726 |
let dst = b.alloc_ssa(RegFile::Pred); |
| 3727 |
|
| 3728 |
if src_bits == 1 { |
| 3729 |
b.push_op(OpVote { |
| 3730 |
op: match intrin.intrinsic { |
| 3731 |
nir_intrinsic_vote_all => VoteOp::All, |
| 3732 |
nir_intrinsic_vote_any => VoteOp::Any, |
| 3733 |
nir_intrinsic_vote_ieq => VoteOp::Eq, |
| 3734 |
_ => panic!("Unknown vote intrinsic"), |
| 3735 |
}, |
| 3736 |
ballot: Dst::None, |
| 3737 |
vote: dst.into(), |
| 3738 |
pred: src, |
| 3739 |
}); |
| 3740 |
} else { |
| 3741 |
assert_eq!(intrin.intrinsic, nir_intrinsic_vote_ieq); |
| 3742 |
b.push_op(OpMatch { |
| 3743 |
op: MatchOp::All, |
| 3744 |
mask: Dst::None, |
| 3745 |
pred: dst.into(), |
| 3746 |
src, |
| 3747 |
u64: match src_bits { |
| 3748 |
32 => false, |
| 3749 |
64 => true, |
| 3750 |
_ => panic!("Unsupported vote_ieq bit size"), |
| 3751 |
}, |
| 3752 |
}); |
| 3753 |
} |
| 3754 |
self.set_dst(&intrin.def, dst.into()); |
| 3755 |
} |
| 3756 |
nir_intrinsic_is_sparse_texels_resident => { |
| 3757 |
let src = self.get_src(&srcs[0]); |
| 3758 |
let dst = b.isetp(IntCmpType::I32, IntCmpOp::Ne, src, 0.into()); |
| 3759 |
self.set_dst(&intrin.def, dst.into()); |
| 3760 |
} |
| 3761 |
nir_intrinsic_cmat_load_shared_nv => { |
| 3762 |
let dst_bit_size = usize::from(intrin.def.bit_size()); |
| 3763 |
let layout: glsl_matrix_layout = intrin.matrix_layout(); |
| 3764 |
let mat_count = intrin.num_matrices(); |
| 3765 |
let dst_num_components = |
| 3766 |
usize::from(intrin.def.num_components()); |
| 3767 |
let comps = |
| 3768 |
(dst_bit_size * dst_num_components).div_ceil(32) as u8; |
| 3769 |
let mat_size = if layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR { |
| 3770 |
LdsmSize::MT8N8 |
| 3771 |
} else { |
| 3772 |
LdsmSize::M8N8 |
| 3773 |
}; |
| 3774 |
let dst = b.alloc_ssa_vec(RegFile::GPR, comps); |
| 3775 |
let (addr, offset) = self.get_io_addr_offset(&srcs[0], 24); |
| 3776 |
let offset = offset + intrin.base(); |
| 3777 |
b.push_op(OpLdsm { |
| 3778 |
dst: dst.clone().into(), |
| 3779 |
mat_size, |
| 3780 |
mat_count, |
| 3781 |
addr, |
| 3782 |
offset, |
| 3783 |
}); |
| 3784 |
self.set_dst(&intrin.def, dst); |
| 3785 |
} |
| 3786 |
nir_intrinsic_cmat_mov_transpose_nv => { |
| 3787 |
let dst = b.alloc_ssa(RegFile::GPR); |
| 3788 |
let src = self.get_src(&srcs[0]); |
| 3789 |
b.push_op(OpMovm { |
| 3790 |
dst: dst.into(), |
| 3791 |
src, |
| 3792 |
}); |
| 3793 |
self.set_dst(&intrin.def, dst.into()); |
| 3794 |
} |
| 3795 |
nir_intrinsic_cmat_muladd_nv => { |
| 3796 |
let flags: nak_nir_cmat_mul_add_flags = |
| 3797 |
unsafe { std::mem::transmute(intrin.flags()) }; |
| 3798 |
let cmat_a = self.get_src(&srcs[0]); |
| 3799 |
let cmat_b = self.get_src(&srcs[1]); |
| 3800 |
let cmat_c = self.get_src(&srcs[2]); |
| 3801 |
let dst_bit_size = intrin.def.bit_size(); |
| 3802 |
let dst = b.alloc_ssa_vec( |
| 3803 |
RegFile::GPR, |
| 3804 |
(intrin.def.num_components() * intrin.def.bit_size) |
| 3805 |
.div_ceil(32), |
| 3806 |
); |
| 3807 |
let dst_type = FloatType::from_bits(dst_bit_size.into()); |
| 3808 |
match flags.cmat_type() { |
| 3809 |
NAK_CMAT_TYPE_M16N8K8_FLOAT |
| 3810 |
| NAK_CMAT_TYPE_M16N8K16_FLOAT => { |
| 3811 |
let mat_size = match flags.cmat_type() { |
| 3812 |
NAK_CMAT_TYPE_M16N8K8_FLOAT => HmmaSize::M16N8K8, |
| 3813 |
NAK_CMAT_TYPE_M16N8K16_FLOAT => HmmaSize::M16N8K16, |
| 3814 |
val => unreachable!("unsupported HMMA type: {val}"), |
| 3815 |
}; |
| 3816 |
|
| 3817 |
assert_eq!(flags.a_type(), GLSL_TYPE_FLOAT16); |
| 3818 |
assert_eq!(flags.b_type(), GLSL_TYPE_FLOAT16); |
| 3819 |
assert!(!flags.sat()); |
| 3820 |
b.push_op(OpHmma { |
| 3821 |
dst: dst.clone().into(), |
| 3822 |
dst_type: dst_type, |
| 3823 |
src_type: FloatType::F16, |
| 3824 |
mat_size: mat_size, |
| 3825 |
srcs: [cmat_a, cmat_b, cmat_c], |
| 3826 |
}); |
| 3827 |
} |
| 3828 |
NAK_CMAT_TYPE_M8N8K16_INT |
| 3829 |
| NAK_CMAT_TYPE_M16N8K16_INT |
| 3830 |
| NAK_CMAT_TYPE_M16N8K32_INT => { |
| 3831 |
let a_type = match flags.a_type() { |
| 3832 |
GLSL_TYPE_UINT8 => IntType::U8, |
| 3833 |
GLSL_TYPE_INT8 => IntType::I8, |
| 3834 |
val => unreachable!("Invalid a_type: {val}"), |
| 3835 |
}; |
| 3836 |
let b_type = match flags.b_type() { |
| 3837 |
GLSL_TYPE_UINT8 => IntType::U8, |
| 3838 |
GLSL_TYPE_INT8 => IntType::I8, |
| 3839 |
val => unreachable!("Invalid b_type: {val}"), |
| 3840 |
}; |
| 3841 |
|
| 3842 |
let mat_size = match flags.cmat_type() { |
| 3843 |
NAK_CMAT_TYPE_M8N8K16_INT => ImmaSize::M8N8K16, |
| 3844 |
NAK_CMAT_TYPE_M16N8K16_INT => ImmaSize::M16N8K16, |
| 3845 |
NAK_CMAT_TYPE_M16N8K32_INT => ImmaSize::M16N8K32, |
| 3846 |
val => unreachable!("unsupported IMMA type: {val}"), |
| 3847 |
}; |
| 3848 |
|
| 3849 |
b.push_op(OpImma { |
| 3850 |
dst: dst.clone().into(), |
| 3851 |
mat_size, |
| 3852 |
srcs: [cmat_a, cmat_b, cmat_c], |
| 3853 |
src_types: [a_type, b_type], |
| 3854 |
saturate: flags.sat(), |
| 3855 |
}); |
| 3856 |
} |
| 3857 |
val => unreachable!("Unknown cmat_type {val}"), |
| 3858 |
} |
| 3859 |
|
| 3860 |
self.set_dst(&intrin.def, dst); |
| 3861 |
} |
| 3862 |
_ => panic!( |
| 3863 |
"Unsupported intrinsic instruction: {}", |
| 3864 |
intrin.info().name() |
| 3865 |
), |
| 3866 |
} |
| 3867 |
} |
| 3868 |
|
| 3869 |
fn parse_load_const( |
| 3870 |
&mut self, |
| 3871 |
b: &mut impl SSABuilder, |
| 3872 |
load_const: &nir_load_const_instr, |
| 3873 |
) { |
| 3874 |
let values = &load_const.values(); |
| 3875 |
|
| 3876 |
let mut dst = Vec::new(); |
| 3877 |
match load_const.def.bit_size { |
| 3878 |
1 => { |
| 3879 |
for c in 0..load_const.def.num_components { |
| 3880 |
let imm_b1 = unsafe { values[usize::from(c)].b }; |
| 3881 |
dst.push(b.copy(imm_b1.into())); |
| 3882 |
} |
| 3883 |
} |
| 3884 |
8 => { |
| 3885 |
for dw in 0..load_const.def.num_components.div_ceil(4) { |
| 3886 |
let mut imm_u32 = 0; |
| 3887 |
for b in 0..4 { |
| 3888 |
let c = dw * 4 + b; |
| 3889 |
if c < load_const.def.num_components { |
| 3890 |
let imm_u8 = unsafe { values[usize::from(c)].u8_ }; |
| 3891 |
imm_u32 |= u32::from(imm_u8) << (b * 8); |
| 3892 |
} |
| 3893 |
} |
| 3894 |
dst.push(b.copy(imm_u32.into())); |
| 3895 |
} |
| 3896 |
} |
| 3897 |
16 => { |
| 3898 |
for dw in 0..load_const.def.num_components.div_ceil(2) { |
| 3899 |
let mut imm_u32 = 0; |
| 3900 |
for w in 0..2 { |
| 3901 |
let c = dw * 2 + w; |
| 3902 |
if c < load_const.def.num_components { |
| 3903 |
let imm_u16 = |
| 3904 |
unsafe { values[usize::from(c)].u16_ }; |
| 3905 |
imm_u32 |= u32::from(imm_u16) << (w * 16); |
| 3906 |
} |
| 3907 |
} |
| 3908 |
dst.push(b.copy(imm_u32.into())); |
| 3909 |
} |
| 3910 |
} |
| 3911 |
32 => { |
| 3912 |
for c in 0..load_const.def.num_components { |
| 3913 |
let imm_u32 = unsafe { values[usize::from(c)].u32_ }; |
| 3914 |
dst.push(b.copy(imm_u32.into())); |
| 3915 |
} |
| 3916 |
} |
| 3917 |
64 => { |
| 3918 |
for c in 0..load_const.def.num_components { |
| 3919 |
let imm_u64 = unsafe { values[c as usize].u64_ }; |
| 3920 |
dst.push(b.copy((imm_u64 as u32).into())); |
| 3921 |
dst.push(b.copy(((imm_u64 >> 32) as u32).into())); |
| 3922 |
} |
| 3923 |
} |
| 3924 |
_ => panic!("Unknown bit size: {}", load_const.def.bit_size), |
| 3925 |
} |
| 3926 |
|
| 3927 |
self.set_ssa(&load_const.def, dst); |
| 3928 |
} |
| 3929 |
|
| 3930 |
fn parse_undef( |
| 3931 |
&mut self, |
| 3932 |
b: &mut impl SSABuilder, |
| 3933 |
undef: &nir_undef_instr, |
| 3934 |
) { |
| 3935 |
let dst = alloc_ssa_for_nir(b, &undef.def); |
| 3936 |
for c in &dst { |
| 3937 |
b.push_op(OpUndef { dst: (*c).into() }); |
| 3938 |
} |
| 3939 |
self.set_ssa(&undef.def, dst); |
| 3940 |
} |
| 3941 |
|
| 3942 |
fn emit_jump( |
| 3943 |
&mut self, |
| 3944 |
b: &mut impl SSABuilder, |
| 3945 |
nb: &nir_block, |
| 3946 |
target: &nir_block, |
| 3947 |
) { |
| 3948 |
if target.index == self.end_block_id { |
| 3949 |
b.push_op(OpExit {}); |
| 3950 |
} else { |
| 3951 |
self.cfg.add_edge(nb.index, target.index); |
| 3952 |
let target_label = self.get_block_label(target); |
| 3953 |
|
| 3954 |
match self.peek_crs(target) { |
| 3955 |
Some(SyncType::Sync) => { |
| 3956 |
b.push_op(OpSync { |
| 3957 |
target: target_label, |
| 3958 |
}); |
| 3959 |
} |
| 3960 |
Some(SyncType::Brk) => { |
| 3961 |
b.push_op(OpBrk { |
| 3962 |
target: target_label, |
| 3963 |
}); |
| 3964 |
} |
| 3965 |
Some(SyncType::Cont) => { |
| 3966 |
b.push_op(OpCont { |
| 3967 |
target: target_label, |
| 3968 |
}); |
| 3969 |
} |
| 3970 |
None => { |
| 3971 |
b.push_op(OpBra { |
| 3972 |
target: target_label, |
| 3973 |
cond: true.into(), |
| 3974 |
}); |
| 3975 |
} |
| 3976 |
} |
| 3977 |
} |
| 3978 |
} |
| 3979 |
|
| 3980 |
fn emit_pred_jump( |
| 3981 |
&mut self, |
| 3982 |
b: &mut impl SSABuilder, |
| 3983 |
nb: &nir_block, |
| 3984 |
pred: Pred, |
| 3985 |
target: &nir_block, |
| 3986 |
fallthrough: &nir_block, |
| 3987 |
) { |
| 3988 |
// The fall-through edge has to come first |
| 3989 |
self.cfg.add_edge(nb.index, fallthrough.index); |
| 3990 |
let op = if target.index == self.end_block_id { |
| 3991 |
Op::Exit(OpExit {}) |
| 3992 |
} else { |
| 3993 |
self.cfg.add_edge(nb.index, target.index); |
| 3994 |
Op::Bra( |
| 3995 |
OpBra { |
| 3996 |
target: self.get_block_label(target), |
| 3997 |
cond: true.into(), |
| 3998 |
} |
| 3999 |
.into(), |
| 4000 |
) |
| 4001 |
}; |
| 4002 |
b.predicate(pred).push_op(op); |
| 4003 |
} |
| 4004 |
|
| 4005 |
fn parse_block( |
| 4006 |
&mut self, |
| 4007 |
ssa_alloc: &mut SSAValueAllocator, |
| 4008 |
phi_map: &mut PhiAllocMap, |
| 4009 |
nb: &nir_block, |
| 4010 |
) { |
| 4011 |
let sm = self.sm; |
| 4012 |
let mut b = SSAInstrBuilder::new(sm, ssa_alloc); |
| 4013 |
|
| 4014 |
if self.sm.sm() >= 70 && nb.index == 0 && self.nir.info.shared_size > 0 |
| 4015 |
{ |
| 4016 |
// The blob seems to always do a BSYNC before accessing shared |
| 4017 |
// memory. Perhaps this is to ensure that our allocation is |
| 4018 |
// actually available and not in use by another thread? |
| 4019 |
let label = self.label_alloc.alloc(); |
| 4020 |
let bar_clear = b.alloc_ssa(RegFile::Bar); |
| 4021 |
|
| 4022 |
b.push_op(OpBClear { |
| 4023 |
dst: bar_clear.into(), |
| 4024 |
}); |
| 4025 |
|
| 4026 |
let bar = b.alloc_ssa(RegFile::Bar); |
| 4027 |
b.push_op(OpBSSy { |
| 4028 |
bar_out: bar.into(), |
| 4029 |
bar_in: bar_clear.into(), |
| 4030 |
cond: SrcRef::True.into(), |
| 4031 |
target: label, |
| 4032 |
}); |
| 4033 |
|
| 4034 |
b.push_op(OpBSync { |
| 4035 |
bar: bar.into(), |
| 4036 |
cond: SrcRef::True.into(), |
| 4037 |
}); |
| 4038 |
|
| 4039 |
b.push_op(OpNop { label: Some(label) }); |
| 4040 |
} |
| 4041 |
|
| 4042 |
let mut phi = OpPhiDsts::new(); |
| 4043 |
for ni in nb.iter_instr_list() { |
| 4044 |
let Some(np) = ni.as_phi() else { |
| 4045 |
break; |
| 4046 |
}; |
| 4047 |
|
| 4048 |
if DEBUG.annotate() { |
| 4049 |
let annotation = self |
| 4050 |
.nir_instr_printer |
| 4051 |
.instr_to_string(ni) |
| 4052 |
.unwrap() |
| 4053 |
.split_whitespace() |
| 4054 |
.collect::<Vec<_>>() |
| 4055 |
.join(" "); |
| 4056 |
b.push_op(OpAnnotate { |
| 4057 |
annotation: format!("generated by \"{}\"", annotation,), |
| 4058 |
}); |
| 4059 |
} |
| 4060 |
|
| 4061 |
let uniform = !nb.divergent |
| 4062 |
&& self.sm.num_regs(RegFile::UGPR) > 0 |
| 4063 |
&& !DEBUG.no_ugpr() |
| 4064 |
&& !np.def.divergent; |
| 4065 |
|
| 4066 |
// This should be ensured by nak_nir_lower_cf() |
| 4067 |
if uniform { |
| 4068 |
for ps in np.iter_srcs() { |
| 4069 |
assert!(!ps.pred().divergent); |
| 4070 |
} |
| 4071 |
} |
| 4072 |
|
| 4073 |
let mut b = UniformBuilder::new(&mut b, uniform); |
| 4074 |
let dst = alloc_ssa_for_nir(&mut b, np.def.as_def()); |
| 4075 |
for i in 0..dst.len() { |
| 4076 |
let phi_id = phi_map.get_phi(np, i.try_into().unwrap()); |
| 4077 |
phi.dsts.push(phi_id, dst[i].into()); |
| 4078 |
} |
| 4079 |
self.set_ssa(np.def.as_def(), dst); |
| 4080 |
} |
| 4081 |
|
| 4082 |
if !phi.dsts.is_empty() { |
| 4083 |
b.push_op(phi); |
| 4084 |
} |
| 4085 |
|
| 4086 |
if self.sm.sm() < 70 |
| 4087 |
&& nb.cf_node.prev().is_none() |
| 4088 |
&& nb.parent().as_loop().is_some() |
| 4089 |
{ |
| 4090 |
b.push_op(OpPCnt { |
| 4091 |
target: self.get_block_label(nb), |
| 4092 |
}); |
| 4093 |
self.push_crs(nb, SyncType::Cont); |
| 4094 |
} |
| 4095 |
|
| 4096 |
let mut goto = None; |
| 4097 |
for ni in nb.iter_instr_list() { |
| 4098 |
if DEBUG.annotate() && ni.type_ != nir_instr_type_phi { |
| 4099 |
let annotation = self |
| 4100 |
.nir_instr_printer |
| 4101 |
.instr_to_string(ni) |
| 4102 |
.unwrap() |
| 4103 |
.split_whitespace() |
| 4104 |
.collect::<Vec<_>>() |
| 4105 |
.join(" "); |
| 4106 |
b.push_op(OpAnnotate { |
| 4107 |
annotation: format!("generated by \"{}\"", annotation,), |
| 4108 |
}); |
| 4109 |
} |
| 4110 |
|
| 4111 |
let uniform = !nb.divergent |
| 4112 |
&& self.sm.num_regs(RegFile::UGPR) > 0 |
| 4113 |
&& !DEBUG.no_ugpr() |
| 4114 |
&& ni.def().is_some_and(|d| !d.divergent); |
| 4115 |
let mut b = UniformBuilder::new(&mut b, uniform); |
| 4116 |
|
| 4117 |
match ni.type_ { |
| 4118 |
nir_instr_type_alu => { |
| 4119 |
self.parse_alu(&mut b, ni.as_alu().unwrap()) |
| 4120 |
} |
| 4121 |
nir_instr_type_jump => { |
| 4122 |
let jump = ni.as_jump().unwrap(); |
| 4123 |
if jump.type_ == nir_jump_goto |
| 4124 |
|| jump.type_ == nir_jump_goto_if |
| 4125 |
{ |
| 4126 |
goto = Some(jump); |
| 4127 |
} |
| 4128 |
} |
| 4129 |
nir_instr_type_tex => { |
| 4130 |
self.parse_tex(&mut b, ni.as_tex().unwrap()) |
| 4131 |
} |
| 4132 |
nir_instr_type_intrinsic => { |
| 4133 |
self.parse_intrinsic(&mut b, ni.as_intrinsic().unwrap()) |
| 4134 |
} |
| 4135 |
nir_instr_type_load_const => { |
| 4136 |
self.parse_load_const(&mut b, ni.as_load_const().unwrap()) |
| 4137 |
} |
| 4138 |
nir_instr_type_undef => { |
| 4139 |
self.parse_undef(&mut b, ni.as_undef().unwrap()) |
| 4140 |
} |
| 4141 |
nir_instr_type_phi => (), |
| 4142 |
_ => panic!("Unsupported instruction type"), |
| 4143 |
} |
| 4144 |
} |
| 4145 |
|
| 4146 |
if self.sm.sm() < 70 { |
| 4147 |
if let Some(ni) = nb.following_if() { |
| 4148 |
let fb = ni.following_block(); |
| 4149 |
b.push_op(OpSSy { |
| 4150 |
target: self.get_block_label(fb), |
| 4151 |
}); |
| 4152 |
self.push_crs(fb, SyncType::Sync); |
| 4153 |
} else if let Some(nl) = nb.following_loop() { |
| 4154 |
let fb = nl.following_block(); |
| 4155 |
b.push_op(OpPBk { |
| 4156 |
target: self.get_block_label(fb), |
| 4157 |
}); |
| 4158 |
self.push_crs(fb, SyncType::Brk); |
| 4159 |
} |
| 4160 |
} |
| 4161 |
|
| 4162 |
let succ = nb.successors(); |
| 4163 |
for sb in succ { |
| 4164 |
let sb = match sb { |
| 4165 |
Some(b) => b, |
| 4166 |
None => continue, |
| 4167 |
}; |
| 4168 |
|
| 4169 |
let mut phi = OpPhiSrcs::new(); |
| 4170 |
|
| 4171 |
for ni in sb.iter_instr_list() { |
| 4172 |
let Some(np) = ni.as_phi() else { |
| 4173 |
break; |
| 4174 |
}; |
| 4175 |
|
| 4176 |
if DEBUG.annotate() { |
| 4177 |
let annotation = self |
| 4178 |
.nir_instr_printer |
| 4179 |
.instr_to_string(ni) |
| 4180 |
.unwrap() |
| 4181 |
.split_whitespace() |
| 4182 |
.collect::<Vec<_>>() |
| 4183 |
.join(" "); |
| 4184 |
b.push_op(OpAnnotate { |
| 4185 |
annotation: format!("generated by \"{}\"", annotation,), |
| 4186 |
}); |
| 4187 |
} |
| 4188 |
|
| 4189 |
for ps in np.iter_srcs() { |
| 4190 |
if ps.pred().index == nb.index { |
| 4191 |
let src = self.get_src(&ps.src); |
| 4192 |
let src = src.as_ssa().unwrap(); |
| 4193 |
for (i, src) in src.iter().enumerate() { |
| 4194 |
let phi_id = |
| 4195 |
phi_map.get_phi(np, i.try_into().unwrap()); |
| 4196 |
phi.srcs.push(phi_id, (*src).into()); |
| 4197 |
} |
| 4198 |
break; |
| 4199 |
} |
| 4200 |
} |
| 4201 |
} |
| 4202 |
|
| 4203 |
if !phi.srcs.is_empty() { |
| 4204 |
b.push_op(phi); |
| 4205 |
} |
| 4206 |
} |
| 4207 |
|
| 4208 |
if let Some(goto) = goto { |
| 4209 |
let target = goto.target().unwrap(); |
| 4210 |
if goto.type_ == nir_jump_goto { |
| 4211 |
self.emit_jump(&mut b, nb, target); |
| 4212 |
} else { |
| 4213 |
let cond = self.get_ssa(goto.condition.as_def())[0]; |
| 4214 |
let else_target = goto.else_target().unwrap(); |
| 4215 |
|
| 4216 |
/* Next block in the NIR CF list */ |
| 4217 |
let next_block = nb.cf_node.next().unwrap().as_block().unwrap(); |
| 4218 |
|
| 4219 |
if std::ptr::eq(else_target, next_block) { |
| 4220 |
self.emit_pred_jump( |
| 4221 |
&mut b, |
| 4222 |
nb, |
| 4223 |
// This is the branch to jump to the else |
| 4224 |
cond.into(), |
| 4225 |
target, |
| 4226 |
else_target, |
| 4227 |
); |
| 4228 |
} else if std::ptr::eq(target, next_block) { |
| 4229 |
self.emit_pred_jump( |
| 4230 |
&mut b, |
| 4231 |
nb, |
| 4232 |
Pred::from(cond).bnot(), |
| 4233 |
else_target, |
| 4234 |
target, |
| 4235 |
); |
| 4236 |
} else { |
| 4237 |
panic!( |
| 4238 |
"One of the two goto targets must be the next block in \ |
| 4239 |
the NIR CF list" |
| 4240 |
); |
| 4241 |
} |
| 4242 |
} |
| 4243 |
} else { |
| 4244 |
if let Some(ni) = nb.following_if() { |
| 4245 |
let cond = self.get_ssa(ni.condition.as_def())[0]; |
| 4246 |
self.emit_pred_jump( |
| 4247 |
&mut b, |
| 4248 |
nb, |
| 4249 |
// This is the branch to jump to the else |
| 4250 |
Pred::from(cond).bnot(), |
| 4251 |
ni.first_else_block(), |
| 4252 |
ni.first_then_block(), |
| 4253 |
); |
| 4254 |
} else { |
| 4255 |
assert!(succ[1].is_none()); |
| 4256 |
let s0 = succ[0].unwrap(); |
| 4257 |
self.emit_jump(&mut b, nb, s0); |
| 4258 |
} |
| 4259 |
} |
| 4260 |
|
| 4261 |
let bb = BasicBlock { |
| 4262 |
label: self.get_block_label(nb), |
| 4263 |
uniform: !nb.divergent, |
| 4264 |
instrs: b.into_vec(), |
| 4265 |
}; |
| 4266 |
self.cfg.add_node(nb.index, bb); |
| 4267 |
} |
| 4268 |
|
| 4269 |
fn parse_if( |
| 4270 |
&mut self, |
| 4271 |
ssa_alloc: &mut SSAValueAllocator, |
| 4272 |
phi_map: &mut PhiAllocMap, |
| 4273 |
ni: &nir_if, |
| 4274 |
) { |
| 4275 |
self.parse_cf_list(ssa_alloc, phi_map, ni.iter_then_list()); |
| 4276 |
self.parse_cf_list(ssa_alloc, phi_map, ni.iter_else_list()); |
| 4277 |
|
| 4278 |
if self.sm.sm() < 70 { |
| 4279 |
let next_block = ni.cf_node.next().unwrap().as_block().unwrap(); |
| 4280 |
self.pop_crs(next_block, SyncType::Sync); |
| 4281 |
} |
| 4282 |
} |
| 4283 |
|
| 4284 |
fn parse_loop( |
| 4285 |
&mut self, |
| 4286 |
ssa_alloc: &mut SSAValueAllocator, |
| 4287 |
phi_map: &mut PhiAllocMap, |
| 4288 |
nl: &nir_loop, |
| 4289 |
) { |
| 4290 |
self.parse_cf_list(ssa_alloc, phi_map, nl.iter_body()); |
| 4291 |
|
| 4292 |
if self.sm.sm() < 70 { |
| 4293 |
let header = nl.iter_body().next().unwrap().as_block().unwrap(); |
| 4294 |
self.pop_crs(header, SyncType::Cont); |
| 4295 |
let next_block = nl.cf_node.next().unwrap().as_block().unwrap(); |
| 4296 |
self.pop_crs(next_block, SyncType::Brk); |
| 4297 |
} |
| 4298 |
} |
| 4299 |
|
| 4300 |
fn parse_cf_list( |
| 4301 |
&mut self, |
| 4302 |
ssa_alloc: &mut SSAValueAllocator, |
| 4303 |
phi_map: &mut PhiAllocMap, |
| 4304 |
list: ExecListIter<nir_cf_node>, |
| 4305 |
) { |
| 4306 |
for node in list { |
| 4307 |
match node.type_ { |
| 4308 |
nir_cf_node_block => { |
| 4309 |
let nb = node.as_block().unwrap(); |
| 4310 |
self.parse_block(ssa_alloc, phi_map, nb); |
| 4311 |
} |
| 4312 |
nir_cf_node_if => { |
| 4313 |
let ni = node.as_if().unwrap(); |
| 4314 |
self.parse_if(ssa_alloc, phi_map, ni); |
| 4315 |
} |
| 4316 |
nir_cf_node_loop => { |
| 4317 |
let nl = node.as_loop().unwrap(); |
| 4318 |
self.parse_loop(ssa_alloc, phi_map, nl); |
| 4319 |
} |
| 4320 |
_ => panic!("Invalid inner CF node type"), |
| 4321 |
} |
| 4322 |
} |
| 4323 |
} |
| 4324 |
|
| 4325 |
pub fn parse_function_impl(&mut self, nfi: &nir_function_impl) -> Function { |
| 4326 |
let mut ssa_alloc = SSAValueAllocator::new(); |
| 4327 |
let end_nb = nfi.end_block(); |
| 4328 |
self.end_block_id = end_nb.index; |
| 4329 |
|
| 4330 |
let mut phi_alloc = PhiAllocator::new(); |
| 4331 |
let mut phi_map = PhiAllocMap::new(&mut phi_alloc); |
| 4332 |
|
| 4333 |
self.parse_cf_list(&mut ssa_alloc, &mut phi_map, nfi.iter_body()); |
| 4334 |
|
| 4335 |
let cfg = std::mem::take(&mut self.cfg).as_cfg(); |
| 4336 |
assert!(cfg.len() > 0); |
| 4337 |
for i in 0..cfg.len() { |
| 4338 |
if cfg[i].falls_through() { |
| 4339 |
assert!(cfg.succ_indices(i)[0] == i + 1); |
| 4340 |
} |
| 4341 |
} |
| 4342 |
|
| 4343 |
let mut f = Function { |
| 4344 |
ssa_alloc: ssa_alloc, |
| 4345 |
phi_alloc: phi_alloc, |
| 4346 |
blocks: cfg, |
| 4347 |
}; |
| 4348 |
f.repair_ssa(); |
| 4349 |
f |
| 4350 |
} |
| 4351 |
|
| 4352 |
pub fn parse_shader(mut self) -> Shader<'a> { |
| 4353 |
let mut functions = Vec::new(); |
| 4354 |
for nf in self.nir.iter_functions() { |
| 4355 |
if let Some(nfi) = nf.get_impl() { |
| 4356 |
let f = self.parse_function_impl(nfi); |
| 4357 |
functions.push(f); |
| 4358 |
} |
| 4359 |
} |
| 4360 |
|
| 4361 |
// Tessellation evaluation shaders MUST claim to read gl_TessCoord or |
| 4362 |
// the hardware will throw an SPH error. |
| 4363 |
if matches!(self.info.stage, ShaderStageInfo::Tessellation(_)) { |
| 4364 |
match &mut self.info.io { |
| 4365 |
ShaderIoInfo::Vtg(io) => { |
| 4366 |
let tc = NAK_ATTR_TESS_COORD; |
| 4367 |
io.mark_attrs_written(tc..(tc + 8)); |
| 4368 |
} |
| 4369 |
_ => panic!("Tessellation must have ShaderIoInfo::Vtg"), |
| 4370 |
} |
| 4371 |
} |
| 4372 |
|
| 4373 |
Shader { |
| 4374 |
sm: self.sm, |
| 4375 |
info: self.info, |
| 4376 |
functions: functions, |
| 4377 |
} |
| 4378 |
} |
| 4379 |
} |
| 4380 |
|
| 4381 |
pub fn nak_shader_from_nir<'a>( |
| 4382 |
nak: &nak_compiler, |
| 4383 |
ns: &'a nir_shader, |
| 4384 |
sm: &'a ShaderModelInfo, |
| 4385 |
) -> Shader<'a> { |
| 4386 |
ShaderFromNir::new(nak, ns, sm).parse_shader() |
| 4387 |
} |