| 1 |
// Copyright © 2022 Collabora, Ltd. |
| 2 |
// SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
use crate::from_nir::*; |
| 5 |
use crate::ir::{ |
| 6 |
ShaderInfo, ShaderIoInfo, ShaderModel, ShaderModelInfo, ShaderStageInfo, |
| 7 |
}; |
| 8 |
use crate::sph; |
| 9 |
|
| 10 |
use compiler::bindings::*; |
| 11 |
use nak_bindings::*; |
| 12 |
|
| 13 |
use std::cmp::max; |
| 14 |
use std::env; |
| 15 |
use std::ffi::{CStr, CString}; |
| 16 |
use std::fmt::Write; |
| 17 |
use std::os::raw::c_void; |
| 18 |
use std::panic; |
| 19 |
use std::sync::OnceLock; |
| 20 |
|
| 21 |
#[repr(u8)] |
| 22 |
enum DebugFlags { |
| 23 |
Panic, |
| 24 |
Print, |
| 25 |
Serial, |
| 26 |
Spill, |
| 27 |
Annotate, |
| 28 |
NoUgpr, |
| 29 |
Cycles, |
| 30 |
} |
| 31 |
|
| 32 |
pub struct Debug { |
| 33 |
flags: u32, |
| 34 |
} |
| 35 |
|
| 36 |
impl Debug { |
| 37 |
fn new() -> Debug { |
| 38 |
let debug_var = "NAK_DEBUG"; |
| 39 |
let debug_str = match env::var(debug_var) { |
| 40 |
Ok(s) => s, |
| 41 |
Err(_) => { |
| 42 |
return Debug { flags: 0 }; |
| 43 |
} |
| 44 |
}; |
| 45 |
|
| 46 |
let mut flags = 0; |
| 47 |
for flag in debug_str.split(',') { |
| 48 |
match flag.trim() { |
| 49 |
"panic" => flags |= 1 << DebugFlags::Panic as u8, |
| 50 |
"print" => flags |= 1 << DebugFlags::Print as u8, |
| 51 |
"serial" => flags |= 1 << DebugFlags::Serial as u8, |
| 52 |
"spill" => flags |= 1 << DebugFlags::Spill as u8, |
| 53 |
"annotate" => flags |= 1 << DebugFlags::Annotate as u8, |
| 54 |
"nougpr" => flags |= 1 << DebugFlags::NoUgpr as u8, |
| 55 |
"cycles" => flags |= 1 << DebugFlags::Cycles as u8, |
| 56 |
unk => eprintln!("Unknown NAK_DEBUG flag \"{}\"", unk), |
| 57 |
} |
| 58 |
} |
| 59 |
Debug { flags: flags } |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
pub trait GetDebugFlags { |
| 64 |
fn debug_flags(&self) -> u32; |
| 65 |
|
| 66 |
fn panic(&self) -> bool { |
| 67 |
self.debug_flags() & (1 << DebugFlags::Panic as u8) != 0 |
| 68 |
} |
| 69 |
|
| 70 |
fn print(&self) -> bool { |
| 71 |
self.debug_flags() & (1 << DebugFlags::Print as u8) != 0 |
| 72 |
} |
| 73 |
|
| 74 |
fn serial(&self) -> bool { |
| 75 |
self.debug_flags() & (1 << DebugFlags::Serial as u8) != 0 |
| 76 |
} |
| 77 |
|
| 78 |
fn spill(&self) -> bool { |
| 79 |
self.debug_flags() & (1 << DebugFlags::Spill as u8) != 0 |
| 80 |
} |
| 81 |
|
| 82 |
fn annotate(&self) -> bool { |
| 83 |
self.debug_flags() & (1 << DebugFlags::Annotate as u8) != 0 |
| 84 |
} |
| 85 |
|
| 86 |
fn no_ugpr(&self) -> bool { |
| 87 |
self.debug_flags() & (1 << DebugFlags::NoUgpr as u8) != 0 |
| 88 |
} |
| 89 |
|
| 90 |
fn cycles(&self) -> bool { |
| 91 |
self.debug_flags() & (1 << DebugFlags::Cycles as u8) != 0 |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
pub static DEBUG: OnceLock<Debug> = OnceLock::new(); |
| 96 |
|
| 97 |
impl GetDebugFlags for OnceLock<Debug> { |
| 98 |
fn debug_flags(&self) -> u32 { |
| 99 |
self.get_or_init(Debug::new).flags |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
#[no_mangle] |
| 104 |
pub extern "C" fn nak_should_print_nir() -> bool { |
| 105 |
DEBUG.print() |
| 106 |
} |
| 107 |
|
| 108 |
#[no_mangle] |
| 109 |
pub extern "C" fn nak_debug_no_ugpr() -> bool { |
| 110 |
DEBUG.no_ugpr() |
| 111 |
} |
| 112 |
|
| 113 |
fn nir_options(dev: &nv_device_info) -> nir_shader_compiler_options { |
| 114 |
nir_shader_compiler_options { |
| 115 |
lower_fdiv: true, |
| 116 |
fuse_ffma16: true, |
| 117 |
fuse_ffma32: true, |
| 118 |
fuse_ffma64: true, |
| 119 |
lower_flrp16: true, |
| 120 |
lower_flrp32: true, |
| 121 |
lower_flrp64: true, |
| 122 |
lower_fsqrt: dev.sm < 52, |
| 123 |
lower_bitfield_extract: false, |
| 124 |
lower_bitfield_extract8: true, |
| 125 |
lower_bitfield_extract16: true, |
| 126 |
lower_bitfield_insert: true, |
| 127 |
lower_pack_half_2x16: true, |
| 128 |
lower_pack_unorm_2x16: true, |
| 129 |
lower_pack_snorm_2x16: true, |
| 130 |
lower_pack_unorm_4x8: true, |
| 131 |
lower_pack_snorm_4x8: true, |
| 132 |
lower_unpack_half_2x16: true, |
| 133 |
lower_unpack_unorm_2x16: true, |
| 134 |
lower_unpack_snorm_2x16: true, |
| 135 |
lower_unpack_unorm_4x8: true, |
| 136 |
lower_unpack_snorm_4x8: true, |
| 137 |
lower_insert_byte: true, |
| 138 |
lower_insert_word: true, |
| 139 |
lower_cs_local_index_to_id: true, |
| 140 |
lower_device_index_to_zero: true, |
| 141 |
lower_isign: true, |
| 142 |
lower_uadd_sat: dev.sm < 70, |
| 143 |
lower_usub_sat: dev.sm < 70, |
| 144 |
lower_iadd_sat: true, // TODO |
| 145 |
lower_doubles_options: nir_lower_drcp |
| 146 |
| nir_lower_dsqrt |
| 147 |
| nir_lower_drsq |
| 148 |
| nir_lower_dtrunc |
| 149 |
| nir_lower_dfloor |
| 150 |
| nir_lower_dceil |
| 151 |
| nir_lower_dfract |
| 152 |
| nir_lower_dround_even |
| 153 |
| nir_lower_dsat |
| 154 |
| if dev.sm >= 70 { nir_lower_dminmax } else { 0 }, |
| 155 |
lower_int64_options: !(nir_lower_icmp64 |
| 156 |
| nir_lower_iadd64 |
| 157 |
| nir_lower_ineg64 |
| 158 |
| nir_lower_shift64 |
| 159 |
| nir_lower_imul_2x32_64 |
| 160 |
| nir_lower_vote_ieq64 |
| 161 |
| nir_lower_conv64) |
| 162 |
| if dev.sm < 70 { nir_lower_vote_ieq64 } else { 0 } |
| 163 |
| if dev.sm < 32 { nir_lower_shift64 } else { 0 }, |
| 164 |
lower_ldexp: true, |
| 165 |
lower_fmod: true, |
| 166 |
lower_ffract: true, |
| 167 |
lower_fpow: true, |
| 168 |
lower_scmp: true, |
| 169 |
lower_uadd_carry: true, |
| 170 |
lower_usub_borrow: true, |
| 171 |
has_iadd3: dev.sm >= 70, |
| 172 |
has_imad32: dev.sm >= 70, |
| 173 |
has_sdot_4x8: dev.sm >= 70, |
| 174 |
has_udot_4x8: dev.sm >= 70, |
| 175 |
has_sudot_4x8: dev.sm >= 70, |
| 176 |
// We set .ftz on f32 by default so we can support fmulz whenever the client |
| 177 |
// doesn't explicitly request denorms. |
| 178 |
has_fmulz_no_denorms: true, |
| 179 |
has_find_msb_rev: true, |
| 180 |
has_pack_half_2x16_rtz: true, |
| 181 |
has_bfm: dev.sm >= 70, |
| 182 |
discard_is_demote: true, |
| 183 |
|
| 184 |
max_unroll_iterations: 32, |
| 185 |
max_samples: 8, |
| 186 |
scalarize_ddx: true, |
| 187 |
..Default::default() |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
#[no_mangle] |
| 192 |
pub extern "C" fn nak_compiler_create( |
| 193 |
dev: *const nv_device_info, |
| 194 |
) -> *mut nak_compiler { |
| 195 |
assert!(!dev.is_null()); |
| 196 |
let dev = unsafe { &*dev }; |
| 197 |
|
| 198 |
let nak = Box::new(nak_compiler { |
| 199 |
sm: dev.sm, |
| 200 |
warps_per_sm: dev.max_warps_per_mp, |
| 201 |
nir_options: nir_options(dev), |
| 202 |
}); |
| 203 |
|
| 204 |
Box::into_raw(nak) |
| 205 |
} |
| 206 |
|
| 207 |
#[no_mangle] |
| 208 |
pub extern "C" fn nak_compiler_destroy(nak: *mut nak_compiler) { |
| 209 |
unsafe { drop(Box::from_raw(nak)) }; |
| 210 |
} |
| 211 |
|
| 212 |
#[no_mangle] |
| 213 |
pub extern "C" fn nak_debug_flags(_nak: *const nak_compiler) -> u64 { |
| 214 |
DEBUG.debug_flags().into() |
| 215 |
} |
| 216 |
|
| 217 |
#[no_mangle] |
| 218 |
pub extern "C" fn nak_nir_options( |
| 219 |
nak: *const nak_compiler, |
| 220 |
) -> *const nir_shader_compiler_options { |
| 221 |
assert!(!nak.is_null()); |
| 222 |
let nak = unsafe { &*nak }; |
| 223 |
&nak.nir_options |
| 224 |
} |
| 225 |
|
| 226 |
#[repr(C)] |
| 227 |
pub struct ShaderBin { |
| 228 |
pub bin: nak_shader_bin, |
| 229 |
code: Vec<u32>, |
| 230 |
asm: CString, |
| 231 |
} |
| 232 |
|
| 233 |
impl ShaderBin { |
| 234 |
pub fn new( |
| 235 |
sm: &ShaderModelInfo, |
| 236 |
info: &ShaderInfo, |
| 237 |
fs_key: Option<&nak_fs_key>, |
| 238 |
code: Vec<u32>, |
| 239 |
asm: &str, |
| 240 |
) -> ShaderBin { |
| 241 |
let asm = CString::new(asm) |
| 242 |
.expect("NAK assembly has unexpected null characters"); |
| 243 |
|
| 244 |
let c_info = nak_shader_info { |
| 245 |
stage: match info.stage { |
| 246 |
ShaderStageInfo::Compute(_) => MESA_SHADER_COMPUTE, |
| 247 |
ShaderStageInfo::Vertex(_) => MESA_SHADER_VERTEX, |
| 248 |
ShaderStageInfo::Fragment(_) => MESA_SHADER_FRAGMENT, |
| 249 |
ShaderStageInfo::Geometry(_) => MESA_SHADER_GEOMETRY, |
| 250 |
ShaderStageInfo::TessellationInit(_) => MESA_SHADER_TESS_CTRL, |
| 251 |
ShaderStageInfo::Tessellation(_) => MESA_SHADER_TESS_EVAL, |
| 252 |
}, |
| 253 |
sm: sm.sm(), |
| 254 |
num_gprs: { |
| 255 |
max(4, info.num_gprs as u32 + sm.hw_reserved_gprs()) |
| 256 |
.try_into() |
| 257 |
.unwrap() |
| 258 |
}, |
| 259 |
num_control_barriers: info.num_control_barriers, |
| 260 |
_pad0: Default::default(), |
| 261 |
_pad1: Default::default(), |
| 262 |
max_warps_per_sm: info.max_warps_per_sm, |
| 263 |
num_instrs: info.num_instrs, |
| 264 |
num_static_cycles: info.num_static_cycles, |
| 265 |
num_spills_to_mem: info.num_spills_to_mem, |
| 266 |
num_fills_from_mem: info.num_fills_from_mem, |
| 267 |
num_spills_to_reg: info.num_spills_to_reg, |
| 268 |
num_fills_from_reg: info.num_fills_from_reg, |
| 269 |
slm_size: info.slm_size, |
| 270 |
crs_size: sm.crs_size(info.max_crs_depth), |
| 271 |
__bindgen_anon_1: match &info.stage { |
| 272 |
ShaderStageInfo::Compute(cs_info) => { |
| 273 |
nak_shader_info__bindgen_ty_1 { |
| 274 |
cs: nak_shader_info__bindgen_ty_1__bindgen_ty_1 { |
| 275 |
local_size: [ |
| 276 |
cs_info.local_size[0], |
| 277 |
cs_info.local_size[1], |
| 278 |
cs_info.local_size[2], |
| 279 |
], |
| 280 |
smem_size: cs_info.smem_size, |
| 281 |
_pad: Default::default(), |
| 282 |
}, |
| 283 |
} |
| 284 |
} |
| 285 |
ShaderStageInfo::Fragment(fs_info) => { |
| 286 |
let fs_io_info = match &info.io { |
| 287 |
ShaderIoInfo::Fragment(io) => io, |
| 288 |
_ => unreachable!(), |
| 289 |
}; |
| 290 |
nak_shader_info__bindgen_ty_1 { |
| 291 |
fs: nak_shader_info__bindgen_ty_1__bindgen_ty_2 { |
| 292 |
writes_depth: fs_io_info.writes_depth, |
| 293 |
reads_sample_mask: fs_io_info.reads_sample_mask, |
| 294 |
post_depth_coverage: fs_info.post_depth_coverage, |
| 295 |
uses_sample_shading: fs_info.uses_sample_shading, |
| 296 |
early_fragment_tests: fs_info.early_fragment_tests, |
| 297 |
_pad: Default::default(), |
| 298 |
}, |
| 299 |
} |
| 300 |
} |
| 301 |
ShaderStageInfo::Tessellation(ts_info) => { |
| 302 |
nak_shader_info__bindgen_ty_1 { |
| 303 |
ts: nak_shader_info__bindgen_ty_1__bindgen_ty_3 { |
| 304 |
domain: ts_info.domain as u8, |
| 305 |
spacing: ts_info.spacing as u8, |
| 306 |
prims: ts_info.primitives as u8, |
| 307 |
_pad: Default::default(), |
| 308 |
}, |
| 309 |
} |
| 310 |
} |
| 311 |
_ => nak_shader_info__bindgen_ty_1 { |
| 312 |
_pad: Default::default(), |
| 313 |
}, |
| 314 |
}, |
| 315 |
vtg: match &info.io { |
| 316 |
ShaderIoInfo::Vtg(io) => nak_shader_info__bindgen_ty_2 { |
| 317 |
writes_layer: io.attr_written(NAK_ATTR_RT_ARRAY_INDEX), |
| 318 |
writes_point_size: io.attr_written(NAK_ATTR_POINT_SIZE), |
| 319 |
writes_vprs_table_index: io |
| 320 |
.attr_written(NAK_ATTR_VPRS_TABLE_INDEX), |
| 321 |
clip_enable: io.clip_enable, |
| 322 |
cull_enable: io.cull_enable, |
| 323 |
xfb: if let Some(xfb) = &io.xfb { |
| 324 |
**xfb |
| 325 |
} else { |
| 326 |
Default::default() |
| 327 |
}, |
| 328 |
_pad: Default::default(), |
| 329 |
}, |
| 330 |
_ => Default::default(), |
| 331 |
}, |
| 332 |
hdr: sph::encode_header(sm, info, fs_key), |
| 333 |
}; |
| 334 |
|
| 335 |
if DEBUG.print() { |
| 336 |
let stage_name = unsafe { |
| 337 |
let c_name = _mesa_shader_stage_to_string(c_info.stage as u32); |
| 338 |
CStr::from_ptr(c_name).to_str().expect("Invalid UTF-8") |
| 339 |
}; |
| 340 |
|
| 341 |
eprintln!("Stage: {}", stage_name); |
| 342 |
eprintln!("Instruction count: {}", c_info.num_instrs); |
| 343 |
eprintln!("Static cycle count: {}", c_info.num_static_cycles); |
| 344 |
eprintln!("Max warps/SM: {}", c_info.max_warps_per_sm); |
| 345 |
eprintln!("Spills to mem: {}", c_info.num_spills_to_mem); |
| 346 |
eprintln!("Spills to reg: {}", c_info.num_spills_to_reg); |
| 347 |
eprintln!("Fills from mem: {}", c_info.num_fills_from_mem); |
| 348 |
eprintln!("Fills from reg: {}", c_info.num_fills_from_reg); |
| 349 |
eprintln!("Num GPRs: {}", c_info.num_gprs); |
| 350 |
eprintln!("SLM size: {}", c_info.slm_size); |
| 351 |
|
| 352 |
if c_info.stage != MESA_SHADER_COMPUTE { |
| 353 |
eprint_hex("Header", &c_info.hdr); |
| 354 |
} |
| 355 |
|
| 356 |
eprint_hex("Encoded shader", &code); |
| 357 |
} |
| 358 |
|
| 359 |
let bin = nak_shader_bin { |
| 360 |
info: c_info, |
| 361 |
code_size: (code.len() * 4).try_into().unwrap(), |
| 362 |
code: code.as_ptr() as *const c_void, |
| 363 |
asm_str: if asm.is_empty() { |
| 364 |
std::ptr::null() |
| 365 |
} else { |
| 366 |
asm.as_ptr() |
| 367 |
}, |
| 368 |
}; |
| 369 |
ShaderBin { |
| 370 |
bin: bin, |
| 371 |
code: code, |
| 372 |
asm: asm, |
| 373 |
} |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
impl std::ops::Deref for ShaderBin { |
| 378 |
type Target = nak_shader_bin; |
| 379 |
|
| 380 |
fn deref(&self) -> &nak_shader_bin { |
| 381 |
&self.bin |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
#[no_mangle] |
| 386 |
pub extern "C" fn nak_shader_bin_destroy(bin: *mut nak_shader_bin) { |
| 387 |
unsafe { |
| 388 |
_ = Box::from_raw(bin as *mut ShaderBin); |
| 389 |
}; |
| 390 |
} |
| 391 |
|
| 392 |
fn eprint_hex(label: &str, data: &[u32]) { |
| 393 |
eprint!("{}:", label); |
| 394 |
for i in 0..data.len() { |
| 395 |
if (i % 8) == 0 { |
| 396 |
eprintln!(); |
| 397 |
eprint!(" "); |
| 398 |
} |
| 399 |
eprint!(" {:08x}", data[i]); |
| 400 |
} |
| 401 |
eprintln!(); |
| 402 |
} |
| 403 |
|
| 404 |
macro_rules! pass { |
| 405 |
($s: expr, $pass: ident) => { |
| 406 |
$s.$pass(); |
| 407 |
if DEBUG.print() { |
| 408 |
eprintln!("NAK IR after {}:\n{}", stringify!($pass), $s); |
| 409 |
} |
| 410 |
}; |
| 411 |
} |
| 412 |
|
| 413 |
fn nak_compile_shader_internal( |
| 414 |
nir: *mut nir_shader, |
| 415 |
dump_asm: bool, |
| 416 |
nak: *const nak_compiler, |
| 417 |
robust2_modes: nir_variable_mode, |
| 418 |
fs_key: *const nak_fs_key, |
| 419 |
) -> *mut nak_shader_bin { |
| 420 |
unsafe { nak_postprocess_nir(nir, nak, robust2_modes, fs_key) }; |
| 421 |
let nak = unsafe { &*nak }; |
| 422 |
let nir = unsafe { &*nir }; |
| 423 |
let fs_key = if fs_key.is_null() { |
| 424 |
None |
| 425 |
} else { |
| 426 |
Some(unsafe { &*fs_key }) |
| 427 |
}; |
| 428 |
|
| 429 |
let sm = ShaderModelInfo::new(nak.sm, nak.warps_per_sm); |
| 430 |
let mut s = nak_shader_from_nir(nak, nir, &sm); |
| 431 |
|
| 432 |
if DEBUG.print() { |
| 433 |
eprintln!("NAK IR:\n{}", &s); |
| 434 |
} |
| 435 |
|
| 436 |
pass!(s, opt_bar_prop); |
| 437 |
pass!(s, opt_uniform_instrs); |
| 438 |
pass!(s, opt_copy_prop); |
| 439 |
pass!(s, opt_prmt); |
| 440 |
pass!(s, opt_lop); |
| 441 |
pass!(s, opt_copy_prop); |
| 442 |
pass!(s, opt_dce); |
| 443 |
pass!(s, opt_out); |
| 444 |
pass!(s, legalize); |
| 445 |
pass!(s, opt_dce); |
| 446 |
pass!(s, opt_instr_sched_prepass); |
| 447 |
pass!(s, assign_regs); |
| 448 |
pass!(s, lower_par_copies); |
| 449 |
pass!(s, lower_copy_swap); |
| 450 |
if nak.sm >= 70 { |
| 451 |
pass!(s, opt_jump_thread); |
| 452 |
} else { |
| 453 |
pass!(s, opt_crs); |
| 454 |
} |
| 455 |
|
| 456 |
s.remove_annotations(); |
| 457 |
|
| 458 |
pass!(s, opt_instr_sched_postpass); |
| 459 |
pass!(s, calc_instr_deps); |
| 460 |
|
| 461 |
s.gather_info(); |
| 462 |
|
| 463 |
let mut asm = String::new(); |
| 464 |
if dump_asm { |
| 465 |
write!(asm, "{}", s).expect("Failed to dump assembly"); |
| 466 |
} |
| 467 |
|
| 468 |
let code = sm.encode_shader(&s); |
| 469 |
let bin = Box::new(ShaderBin::new(&sm, &s.info, fs_key, code, &asm)); |
| 470 |
Box::into_raw(bin) as *mut nak_shader_bin |
| 471 |
} |
| 472 |
|
| 473 |
#[no_mangle] |
| 474 |
pub extern "C" fn nak_compile_shader( |
| 475 |
nir: *mut nir_shader, |
| 476 |
dump_asm: bool, |
| 477 |
nak: *const nak_compiler, |
| 478 |
robust2_modes: nir_variable_mode, |
| 479 |
fs_key: *const nak_fs_key, |
| 480 |
) -> *mut nak_shader_bin { |
| 481 |
let compile = || { |
| 482 |
nak_compile_shader_internal(nir, dump_asm, nak, robust2_modes, fs_key) |
| 483 |
}; |
| 484 |
if DEBUG.panic() { |
| 485 |
compile() |
| 486 |
} else { |
| 487 |
panic::catch_unwind(compile).unwrap_or(std::ptr::null_mut()) |
| 488 |
} |
| 489 |
} |