Clone of mesa.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /*
  2. * Copyright © 2018 Valve Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. */
  24. #include "aco_ir.h"
  25. #include <array>
  26. #include <map>
  27. namespace aco {
  28. #ifndef NDEBUG
  29. void perfwarn(bool cond, const char *msg, Instruction *instr)
  30. {
  31. if (cond) {
  32. fprintf(stderr, "ACO performance warning: %s\n", msg);
  33. if (instr) {
  34. fprintf(stderr, "instruction: ");
  35. aco_print_instr(instr, stderr);
  36. fprintf(stderr, "\n");
  37. }
  38. if (debug_flags & DEBUG_PERFWARN)
  39. exit(1);
  40. }
  41. }
  42. #endif
  43. void validate(Program* program, FILE * output)
  44. {
  45. if (!(debug_flags & DEBUG_VALIDATE))
  46. return;
  47. bool is_valid = true;
  48. auto check = [&output, &is_valid](bool check, const char * msg, aco::Instruction * instr) -> void {
  49. if (!check) {
  50. fprintf(output, "%s: ", msg);
  51. aco_print_instr(instr, output);
  52. fprintf(output, "\n");
  53. is_valid = false;
  54. }
  55. };
  56. for (Block& block : program->blocks) {
  57. for (aco_ptr<Instruction>& instr : block.instructions) {
  58. /* check base format */
  59. Format base_format = instr->format;
  60. base_format = (Format)((uint32_t)base_format & ~(uint32_t)Format::SDWA);
  61. base_format = (Format)((uint32_t)base_format & ~(uint32_t)Format::DPP);
  62. if ((uint32_t)base_format & (uint32_t)Format::VOP1)
  63. base_format = Format::VOP1;
  64. else if ((uint32_t)base_format & (uint32_t)Format::VOP2)
  65. base_format = Format::VOP2;
  66. else if ((uint32_t)base_format & (uint32_t)Format::VOPC)
  67. base_format = Format::VOPC;
  68. else if ((uint32_t)base_format & (uint32_t)Format::VINTRP)
  69. base_format = Format::VINTRP;
  70. check(base_format == instr_info.format[(int)instr->opcode], "Wrong base format for instruction", instr.get());
  71. /* check VOP3 modifiers */
  72. if (((uint32_t)instr->format & (uint32_t)Format::VOP3) && instr->format != Format::VOP3) {
  73. check(base_format == Format::VOP2 ||
  74. base_format == Format::VOP1 ||
  75. base_format == Format::VOPC ||
  76. base_format == Format::VINTRP,
  77. "Format cannot have VOP3A/VOP3B applied", instr.get());
  78. }
  79. /* check for undefs */
  80. for (unsigned i = 0; i < instr->operands.size(); i++) {
  81. if (instr->operands[i].isUndefined()) {
  82. bool flat = instr->format == Format::FLAT || instr->format == Format::SCRATCH || instr->format == Format::GLOBAL;
  83. bool can_be_undef = is_phi(instr) || instr->format == Format::EXP ||
  84. instr->format == Format::PSEUDO_REDUCTION ||
  85. (flat && i == 1) || (instr->format == Format::MIMG && i == 2) ||
  86. ((instr->format == Format::MUBUF || instr->format == Format::MTBUF) && i == 0);
  87. check(can_be_undef, "Undefs can only be used in certain operands", instr.get());
  88. }
  89. }
  90. /* check num literals */
  91. if (instr->isSALU() || instr->isVALU()) {
  92. unsigned num_literals = 0;
  93. for (unsigned i = 0; i < instr->operands.size(); i++)
  94. {
  95. if (instr->operands[i].isLiteral() && instr->isVOP3() && program->chip_class >= GFX10) {
  96. num_literals++;
  97. } else if (instr->operands[i].isLiteral()) {
  98. check(instr->format == Format::SOP1 ||
  99. instr->format == Format::SOP2 ||
  100. instr->format == Format::SOPC ||
  101. instr->format == Format::VOP1 ||
  102. instr->format == Format::VOP2 ||
  103. instr->format == Format::VOPC,
  104. "Literal applied on wrong instruction format", instr.get());
  105. num_literals++;
  106. check(!instr->isVALU() || i == 0 || i == 2, "Wrong source position for Literal argument", instr.get());
  107. }
  108. }
  109. check(num_literals <= 1, "Only 1 Literal allowed", instr.get());
  110. /* check num sgprs for VALU */
  111. if (instr->isVALU()) {
  112. check(instr->definitions[0].getTemp().type() == RegType::vgpr ||
  113. (int) instr->format & (int) Format::VOPC ||
  114. instr->opcode == aco_opcode::v_readfirstlane_b32 ||
  115. instr->opcode == aco_opcode::v_readlane_b32,
  116. "Wrong Definition type for VALU instruction", instr.get());
  117. unsigned num_sgpr = 0;
  118. unsigned sgpr_idx = instr->operands.size();
  119. for (unsigned i = 0; i < instr->operands.size(); i++)
  120. {
  121. if (instr->operands[i].isTemp() && instr->operands[i].regClass().type() == RegType::sgpr) {
  122. check(i != 1 || (int) instr->format & (int) Format::VOP3A, "Wrong source position for SGPR argument", instr.get());
  123. if (sgpr_idx == instr->operands.size() || instr->operands[sgpr_idx].tempId() != instr->operands[i].tempId())
  124. num_sgpr++;
  125. sgpr_idx = i;
  126. }
  127. if (instr->operands[i].isConstant() && !instr->operands[i].isLiteral())
  128. check(i == 0 || (int) instr->format & (int) Format::VOP3A, "Wrong source position for constant argument", instr.get());
  129. }
  130. check(num_sgpr + num_literals <= 1, "Only 1 Literal OR 1 SGPR allowed", instr.get());
  131. }
  132. if (instr->format == Format::SOP1 || instr->format == Format::SOP2) {
  133. check(instr->definitions[0].getTemp().type() == RegType::sgpr, "Wrong Definition type for SALU instruction", instr.get());
  134. for (const Operand& op : instr->operands) {
  135. check(op.isConstant() || op.regClass().type() <= RegType::sgpr,
  136. "Wrong Operand type for SALU instruction", instr.get());
  137. }
  138. }
  139. }
  140. switch (instr->format) {
  141. case Format::PSEUDO: {
  142. if (instr->opcode == aco_opcode::p_create_vector) {
  143. unsigned size = 0;
  144. for (const Operand& op : instr->operands) {
  145. size += op.size();
  146. }
  147. check(size == instr->definitions[0].size(), "Definition size does not match operand sizes", instr.get());
  148. if (instr->definitions[0].getTemp().type() == RegType::sgpr) {
  149. for (const Operand& op : instr->operands) {
  150. check(op.isConstant() || op.regClass().type() == RegType::sgpr,
  151. "Wrong Operand type for scalar vector", instr.get());
  152. }
  153. }
  154. } else if (instr->opcode == aco_opcode::p_extract_vector) {
  155. check((instr->operands[0].isTemp()) && instr->operands[1].isConstant(), "Wrong Operand types", instr.get());
  156. check(instr->operands[1].constantValue() < instr->operands[0].size(), "Index out of range", instr.get());
  157. check(instr->definitions[0].getTemp().type() == RegType::vgpr || instr->operands[0].regClass().type() == RegType::sgpr,
  158. "Cannot extract SGPR value from VGPR vector", instr.get());
  159. } else if (instr->opcode == aco_opcode::p_parallelcopy) {
  160. check(instr->definitions.size() == instr->operands.size(), "Number of Operands does not match number of Definitions", instr.get());
  161. for (unsigned i = 0; i < instr->operands.size(); i++) {
  162. if (instr->operands[i].isTemp())
  163. check((instr->definitions[i].getTemp().type() == instr->operands[i].regClass().type()) ||
  164. (instr->definitions[i].getTemp().type() == RegType::vgpr && instr->operands[i].regClass().type() == RegType::sgpr),
  165. "Operand and Definition types do not match", instr.get());
  166. }
  167. } else if (instr->opcode == aco_opcode::p_phi) {
  168. check(instr->operands.size() == block.logical_preds.size(), "Number of Operands does not match number of predecessors", instr.get());
  169. check(instr->definitions[0].getTemp().type() == RegType::vgpr || instr->definitions[0].getTemp().regClass() == s2, "Logical Phi Definition must be vgpr or divergent boolean", instr.get());
  170. } else if (instr->opcode == aco_opcode::p_linear_phi) {
  171. for (const Operand& op : instr->operands)
  172. check(!op.isTemp() || op.getTemp().is_linear(), "Wrong Operand type", instr.get());
  173. check(instr->operands.size() == block.linear_preds.size(), "Number of Operands does not match number of predecessors", instr.get());
  174. }
  175. break;
  176. }
  177. case Format::SMEM: {
  178. if (instr->operands.size() >= 1)
  179. check(instr->operands[0].isTemp() && instr->operands[0].regClass().type() == RegType::sgpr, "SMEM operands must be sgpr", instr.get());
  180. if (instr->operands.size() >= 2)
  181. check(instr->operands[1].isConstant() || (instr->operands[1].isTemp() && instr->operands[1].regClass().type() == RegType::sgpr),
  182. "SMEM offset must be constant or sgpr", instr.get());
  183. if (!instr->definitions.empty())
  184. check(instr->definitions[0].getTemp().type() == RegType::sgpr, "SMEM result must be sgpr", instr.get());
  185. break;
  186. }
  187. case Format::MTBUF:
  188. case Format::MUBUF:
  189. case Format::MIMG: {
  190. check(instr->operands.size() > 1, "VMEM instructions must have at least one operand", instr.get());
  191. check(instr->operands[0].hasRegClass() && instr->operands[0].regClass().type() == RegType::vgpr,
  192. "VADDR must be in vgpr for VMEM instructions", instr.get());
  193. check(instr->operands[1].isTemp() && instr->operands[1].regClass().type() == RegType::sgpr, "VMEM resource constant must be sgpr", instr.get());
  194. check(instr->operands.size() < 4 || (instr->operands[3].isTemp() && instr->operands[3].regClass().type() == RegType::vgpr), "VMEM write data must be vgpr", instr.get());
  195. break;
  196. }
  197. case Format::DS: {
  198. for (const Operand& op : instr->operands) {
  199. check((op.isTemp() && op.regClass().type() == RegType::vgpr) || op.physReg() == m0,
  200. "Only VGPRs are valid DS instruction operands", instr.get());
  201. }
  202. if (!instr->definitions.empty())
  203. check(instr->definitions[0].getTemp().type() == RegType::vgpr, "DS instruction must return VGPR", instr.get());
  204. break;
  205. }
  206. case Format::EXP: {
  207. for (unsigned i = 0; i < 4; i++)
  208. check(instr->operands[i].hasRegClass() && instr->operands[i].regClass().type() == RegType::vgpr,
  209. "Only VGPRs are valid Export arguments", instr.get());
  210. break;
  211. }
  212. case Format::FLAT:
  213. check(instr->operands[1].isUndefined(), "Flat instructions don't support SADDR", instr.get());
  214. /* fallthrough */
  215. case Format::GLOBAL:
  216. case Format::SCRATCH: {
  217. check(instr->operands[0].isTemp() && instr->operands[0].regClass().type() == RegType::vgpr, "FLAT/GLOBAL/SCRATCH address must be vgpr", instr.get());
  218. check(instr->operands[1].hasRegClass() && instr->operands[1].regClass().type() == RegType::sgpr,
  219. "FLAT/GLOBAL/SCRATCH sgpr address must be undefined or sgpr", instr.get());
  220. if (!instr->definitions.empty())
  221. check(instr->definitions[0].getTemp().type() == RegType::vgpr, "FLAT/GLOBAL/SCRATCH result must be vgpr", instr.get());
  222. else
  223. check(instr->operands[2].regClass().type() == RegType::vgpr, "FLAT/GLOBAL/SCRATCH data must be vgpr", instr.get());
  224. break;
  225. }
  226. default:
  227. break;
  228. }
  229. }
  230. }
  231. assert(is_valid);
  232. }
  233. /* RA validation */
  234. namespace {
  235. struct Location {
  236. Location() : block(NULL), instr(NULL) {}
  237. Block *block;
  238. Instruction *instr; //NULL if it's the block's live-in
  239. };
  240. struct Assignment {
  241. Location defloc;
  242. Location firstloc;
  243. PhysReg reg;
  244. };
  245. bool ra_fail(FILE *output, Location loc, Location loc2, const char *fmt, ...) {
  246. va_list args;
  247. va_start(args, fmt);
  248. char msg[1024];
  249. vsprintf(msg, fmt, args);
  250. va_end(args);
  251. fprintf(stderr, "RA error found at instruction in BB%d:\n", loc.block->index);
  252. if (loc.instr) {
  253. aco_print_instr(loc.instr, stderr);
  254. fprintf(stderr, "\n%s", msg);
  255. } else {
  256. fprintf(stderr, "%s", msg);
  257. }
  258. if (loc2.block) {
  259. fprintf(stderr, " in BB%d:\n", loc2.block->index);
  260. aco_print_instr(loc2.instr, stderr);
  261. }
  262. fprintf(stderr, "\n\n");
  263. return true;
  264. }
  265. } /* end namespace */
  266. bool validate_ra(Program *program, const struct radv_nir_compiler_options *options, FILE *output) {
  267. if (!(debug_flags & DEBUG_VALIDATE_RA))
  268. return false;
  269. bool err = false;
  270. aco::live live_vars = aco::live_var_analysis(program, options);
  271. std::vector<std::vector<Temp>> phi_sgpr_ops(program->blocks.size());
  272. std::map<unsigned, Assignment> assignments;
  273. for (Block& block : program->blocks) {
  274. Location loc;
  275. loc.block = &block;
  276. for (aco_ptr<Instruction>& instr : block.instructions) {
  277. if (instr->opcode == aco_opcode::p_phi) {
  278. for (unsigned i = 0; i < instr->operands.size(); i++) {
  279. if (instr->operands[i].isTemp() &&
  280. instr->operands[i].getTemp().type() == RegType::sgpr &&
  281. instr->operands[i].isFirstKill())
  282. phi_sgpr_ops[block.logical_preds[i]].emplace_back(instr->operands[i].getTemp());
  283. }
  284. }
  285. loc.instr = instr.get();
  286. for (unsigned i = 0; i < instr->operands.size(); i++) {
  287. Operand& op = instr->operands[i];
  288. if (!op.isTemp())
  289. continue;
  290. if (!op.isFixed())
  291. err |= ra_fail(output, loc, Location(), "Operand %d is not assigned a register", i);
  292. if (assignments.count(op.tempId()) && assignments[op.tempId()].reg != op.physReg())
  293. err |= ra_fail(output, loc, assignments.at(op.tempId()).firstloc, "Operand %d has an inconsistent register assignment with instruction", i);
  294. if ((op.getTemp().type() == RegType::vgpr && op.physReg() + op.size() > 256 + program->config->num_vgprs) ||
  295. (op.getTemp().type() == RegType::sgpr && op.physReg() + op.size() > program->config->num_sgprs && op.physReg() < program->sgpr_limit))
  296. err |= ra_fail(output, loc, assignments.at(op.tempId()).firstloc, "Operand %d has an out-of-bounds register assignment", i);
  297. if (!assignments[op.tempId()].firstloc.block)
  298. assignments[op.tempId()].firstloc = loc;
  299. if (!assignments[op.tempId()].defloc.block)
  300. assignments[op.tempId()].reg = op.physReg();
  301. }
  302. for (unsigned i = 0; i < instr->definitions.size(); i++) {
  303. Definition& def = instr->definitions[i];
  304. if (!def.isTemp())
  305. continue;
  306. if (!def.isFixed())
  307. err |= ra_fail(output, loc, Location(), "Definition %d is not assigned a register", i);
  308. if (assignments[def.tempId()].defloc.block)
  309. err |= ra_fail(output, loc, assignments.at(def.tempId()).defloc, "Temporary %%%d also defined by instruction", def.tempId());
  310. if ((def.getTemp().type() == RegType::vgpr && def.physReg() + def.size() > 256 + program->config->num_vgprs) ||
  311. (def.getTemp().type() == RegType::sgpr && def.physReg() + def.size() > program->config->num_sgprs && def.physReg() < program->sgpr_limit))
  312. err |= ra_fail(output, loc, assignments.at(def.tempId()).firstloc, "Definition %d has an out-of-bounds register assignment", i);
  313. if (!assignments[def.tempId()].firstloc.block)
  314. assignments[def.tempId()].firstloc = loc;
  315. assignments[def.tempId()].defloc = loc;
  316. assignments[def.tempId()].reg = def.physReg();
  317. }
  318. }
  319. }
  320. for (Block& block : program->blocks) {
  321. Location loc;
  322. loc.block = &block;
  323. std::array<unsigned, 512> regs;
  324. regs.fill(0);
  325. std::set<Temp> live;
  326. live.insert(live_vars.live_out[block.index].begin(), live_vars.live_out[block.index].end());
  327. /* remove killed p_phi sgpr operands */
  328. for (Temp tmp : phi_sgpr_ops[block.index])
  329. live.erase(tmp);
  330. /* check live out */
  331. for (Temp tmp : live) {
  332. PhysReg reg = assignments.at(tmp.id()).reg;
  333. for (unsigned i = 0; i < tmp.size(); i++) {
  334. if (regs[reg + i]) {
  335. err |= ra_fail(output, loc, Location(), "Assignment of element %d of %%%d already taken by %%%d in live-out", i, tmp.id(), regs[reg + i]);
  336. }
  337. regs[reg + i] = tmp.id();
  338. }
  339. }
  340. regs.fill(0);
  341. for (auto it = block.instructions.rbegin(); it != block.instructions.rend(); ++it) {
  342. aco_ptr<Instruction>& instr = *it;
  343. /* check killed p_phi sgpr operands */
  344. if (instr->opcode == aco_opcode::p_logical_end) {
  345. for (Temp tmp : phi_sgpr_ops[block.index]) {
  346. PhysReg reg = assignments.at(tmp.id()).reg;
  347. for (unsigned i = 0; i < tmp.size(); i++) {
  348. if (regs[reg + i])
  349. err |= ra_fail(output, loc, Location(), "Assignment of element %d of %%%d already taken by %%%d in live-out", i, tmp.id(), regs[reg + i]);
  350. }
  351. live.emplace(tmp);
  352. }
  353. }
  354. for (const Definition& def : instr->definitions) {
  355. if (!def.isTemp())
  356. continue;
  357. live.erase(def.getTemp());
  358. }
  359. /* don't count phi operands as live-in, since they are actually
  360. * killed when they are copied at the predecessor */
  361. if (instr->opcode != aco_opcode::p_phi && instr->opcode != aco_opcode::p_linear_phi) {
  362. for (const Operand& op : instr->operands) {
  363. if (!op.isTemp())
  364. continue;
  365. live.insert(op.getTemp());
  366. }
  367. }
  368. }
  369. for (Temp tmp : live) {
  370. PhysReg reg = assignments.at(tmp.id()).reg;
  371. for (unsigned i = 0; i < tmp.size(); i++)
  372. regs[reg + i] = tmp.id();
  373. }
  374. for (aco_ptr<Instruction>& instr : block.instructions) {
  375. loc.instr = instr.get();
  376. /* remove killed p_phi operands from regs */
  377. if (instr->opcode == aco_opcode::p_logical_end) {
  378. for (Temp tmp : phi_sgpr_ops[block.index]) {
  379. PhysReg reg = assignments.at(tmp.id()).reg;
  380. regs[reg] = 0;
  381. }
  382. }
  383. if (instr->opcode != aco_opcode::p_phi && instr->opcode != aco_opcode::p_linear_phi) {
  384. for (const Operand& op : instr->operands) {
  385. if (!op.isTemp())
  386. continue;
  387. if (op.isFirstKill()) {
  388. for (unsigned j = 0; j < op.getTemp().size(); j++)
  389. regs[op.physReg() + j] = 0;
  390. }
  391. }
  392. }
  393. for (unsigned i = 0; i < instr->definitions.size(); i++) {
  394. Definition& def = instr->definitions[i];
  395. if (!def.isTemp())
  396. continue;
  397. Temp tmp = def.getTemp();
  398. PhysReg reg = assignments.at(tmp.id()).reg;
  399. for (unsigned j = 0; j < tmp.size(); j++) {
  400. if (regs[reg + j])
  401. err |= ra_fail(output, loc, assignments.at(regs[reg + i]).defloc, "Assignment of element %d of %%%d already taken by %%%d from instruction", i, tmp.id(), regs[reg + j]);
  402. regs[reg + j] = tmp.id();
  403. }
  404. }
  405. for (const Definition& def : instr->definitions) {
  406. if (!def.isTemp())
  407. continue;
  408. if (def.isKill()) {
  409. for (unsigned j = 0; j < def.getTemp().size(); j++)
  410. regs[def.physReg() + j] = 0;
  411. }
  412. }
  413. }
  414. }
  415. return err;
  416. }
  417. }