ir_emitter.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  1. // Copyright 2021 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "common/bit_cast.h"
  5. #include "shader_recompiler/frontend/ir/ir_emitter.h"
  6. #include "shader_recompiler/frontend/ir/value.h"
  7. namespace Shader::IR {
  8. [[noreturn]] static void ThrowInvalidType(Type type) {
  9. throw InvalidArgument("Invalid type {}", type);
  10. }
  11. U1 IREmitter::Imm1(bool value) const {
  12. return U1{Value{value}};
  13. }
  14. U8 IREmitter::Imm8(u8 value) const {
  15. return U8{Value{value}};
  16. }
  17. U16 IREmitter::Imm16(u16 value) const {
  18. return U16{Value{value}};
  19. }
  20. U32 IREmitter::Imm32(u32 value) const {
  21. return U32{Value{value}};
  22. }
  23. U32 IREmitter::Imm32(s32 value) const {
  24. return U32{Value{static_cast<u32>(value)}};
  25. }
  26. F32 IREmitter::Imm32(f32 value) const {
  27. return F32{Value{value}};
  28. }
  29. U64 IREmitter::Imm64(u64 value) const {
  30. return U64{Value{value}};
  31. }
  32. F64 IREmitter::Imm64(f64 value) const {
  33. return F64{Value{value}};
  34. }
  35. void IREmitter::Branch(Block* label) {
  36. label->AddImmediatePredecessor(block);
  37. block->SetBranch(label);
  38. Inst(Opcode::Branch, label);
  39. }
  40. void IREmitter::BranchConditional(const U1& condition, Block* true_label, Block* false_label) {
  41. block->SetBranches(IR::Condition{true}, true_label, false_label);
  42. true_label->AddImmediatePredecessor(block);
  43. false_label->AddImmediatePredecessor(block);
  44. Inst(Opcode::BranchConditional, condition, true_label, false_label);
  45. }
  46. void IREmitter::LoopMerge(Block* merge_block, Block* continue_target) {
  47. Inst(Opcode::LoopMerge, merge_block, continue_target);
  48. }
  49. void IREmitter::SelectionMerge(Block* merge_block) {
  50. Inst(Opcode::SelectionMerge, merge_block);
  51. }
  52. void IREmitter::Return() {
  53. Inst(Opcode::Return);
  54. }
  55. U32 IREmitter::GetReg(IR::Reg reg) {
  56. return Inst<U32>(Opcode::GetRegister, reg);
  57. }
  58. void IREmitter::SetReg(IR::Reg reg, const U32& value) {
  59. Inst(Opcode::SetRegister, reg, value);
  60. }
  61. U1 IREmitter::GetPred(IR::Pred pred, bool is_negated) {
  62. const U1 value{Inst<U1>(Opcode::GetPred, pred)};
  63. if (is_negated) {
  64. return Inst<U1>(Opcode::LogicalNot, value);
  65. } else {
  66. return value;
  67. }
  68. }
  69. U1 IREmitter::GetGotoVariable(u32 id) {
  70. return Inst<U1>(Opcode::GetGotoVariable, id);
  71. }
  72. void IREmitter::SetGotoVariable(u32 id, const U1& value) {
  73. Inst(Opcode::SetGotoVariable, id, value);
  74. }
  75. void IREmitter::SetPred(IR::Pred pred, const U1& value) {
  76. Inst(Opcode::SetPred, pred, value);
  77. }
  78. U32 IREmitter::GetCbuf(const U32& binding, const U32& byte_offset) {
  79. return Inst<U32>(Opcode::GetCbuf, binding, byte_offset);
  80. }
  81. U1 IREmitter::GetZFlag() {
  82. return Inst<U1>(Opcode::GetZFlag);
  83. }
  84. U1 IREmitter::GetSFlag() {
  85. return Inst<U1>(Opcode::GetSFlag);
  86. }
  87. U1 IREmitter::GetCFlag() {
  88. return Inst<U1>(Opcode::GetCFlag);
  89. }
  90. U1 IREmitter::GetOFlag() {
  91. return Inst<U1>(Opcode::GetOFlag);
  92. }
  93. void IREmitter::SetZFlag(const U1& value) {
  94. Inst(Opcode::SetZFlag, value);
  95. }
  96. void IREmitter::SetSFlag(const U1& value) {
  97. Inst(Opcode::SetSFlag, value);
  98. }
  99. void IREmitter::SetCFlag(const U1& value) {
  100. Inst(Opcode::SetCFlag, value);
  101. }
  102. void IREmitter::SetOFlag(const U1& value) {
  103. Inst(Opcode::SetOFlag, value);
  104. }
  105. static U1 GetFlowTest(IREmitter& ir, FlowTest flow_test) {
  106. switch (flow_test) {
  107. case FlowTest::T:
  108. return ir.Imm1(true);
  109. case FlowTest::F:
  110. return ir.Imm1(false);
  111. case FlowTest::EQ:
  112. // TODO: Test this
  113. return ir.GetZFlag();
  114. case FlowTest::NE:
  115. // TODO: Test this
  116. return ir.LogicalNot(ir.GetZFlag());
  117. default:
  118. throw NotImplementedException("Flow test {}", flow_test);
  119. }
  120. }
  121. U1 IREmitter::Condition(IR::Condition cond) {
  122. const FlowTest flow_test{cond.FlowTest()};
  123. const auto [pred, is_negated]{cond.Pred()};
  124. return LogicalAnd(GetPred(pred, is_negated), GetFlowTest(*this, flow_test));
  125. }
  126. F32 IREmitter::GetAttribute(IR::Attribute attribute) {
  127. return Inst<F32>(Opcode::GetAttribute, attribute);
  128. }
  129. void IREmitter::SetAttribute(IR::Attribute attribute, const F32& value) {
  130. Inst(Opcode::SetAttribute, attribute, value);
  131. }
  132. U32 IREmitter::WorkgroupIdX() {
  133. return U32{CompositeExtract(Inst(Opcode::WorkgroupId), 0)};
  134. }
  135. U32 IREmitter::WorkgroupIdY() {
  136. return U32{CompositeExtract(Inst(Opcode::WorkgroupId), 1)};
  137. }
  138. U32 IREmitter::WorkgroupIdZ() {
  139. return U32{CompositeExtract(Inst(Opcode::WorkgroupId), 2)};
  140. }
  141. U32 IREmitter::LocalInvocationIdX() {
  142. return U32{CompositeExtract(Inst(Opcode::LocalInvocationId), 0)};
  143. }
  144. U32 IREmitter::LocalInvocationIdY() {
  145. return U32{CompositeExtract(Inst(Opcode::LocalInvocationId), 1)};
  146. }
  147. U32 IREmitter::LocalInvocationIdZ() {
  148. return U32{CompositeExtract(Inst(Opcode::LocalInvocationId), 2)};
  149. }
  150. U32 IREmitter::LoadGlobalU8(const U64& address) {
  151. return Inst<U32>(Opcode::LoadGlobalU8, address);
  152. }
  153. U32 IREmitter::LoadGlobalS8(const U64& address) {
  154. return Inst<U32>(Opcode::LoadGlobalS8, address);
  155. }
  156. U32 IREmitter::LoadGlobalU16(const U64& address) {
  157. return Inst<U32>(Opcode::LoadGlobalU16, address);
  158. }
  159. U32 IREmitter::LoadGlobalS16(const U64& address) {
  160. return Inst<U32>(Opcode::LoadGlobalS16, address);
  161. }
  162. U32 IREmitter::LoadGlobal32(const U64& address) {
  163. return Inst<U32>(Opcode::LoadGlobal32, address);
  164. }
  165. Value IREmitter::LoadGlobal64(const U64& address) {
  166. return Inst<Value>(Opcode::LoadGlobal64, address);
  167. }
  168. Value IREmitter::LoadGlobal128(const U64& address) {
  169. return Inst<Value>(Opcode::LoadGlobal128, address);
  170. }
  171. void IREmitter::WriteGlobalU8(const U64& address, const U32& value) {
  172. Inst(Opcode::WriteGlobalU8, address, value);
  173. }
  174. void IREmitter::WriteGlobalS8(const U64& address, const U32& value) {
  175. Inst(Opcode::WriteGlobalS8, address, value);
  176. }
  177. void IREmitter::WriteGlobalU16(const U64& address, const U32& value) {
  178. Inst(Opcode::WriteGlobalU16, address, value);
  179. }
  180. void IREmitter::WriteGlobalS16(const U64& address, const U32& value) {
  181. Inst(Opcode::WriteGlobalS16, address, value);
  182. }
  183. void IREmitter::WriteGlobal32(const U64& address, const U32& value) {
  184. Inst(Opcode::WriteGlobal32, address, value);
  185. }
  186. void IREmitter::WriteGlobal64(const U64& address, const IR::Value& vector) {
  187. Inst(Opcode::WriteGlobal64, address, vector);
  188. }
  189. void IREmitter::WriteGlobal128(const U64& address, const IR::Value& vector) {
  190. Inst(Opcode::WriteGlobal128, address, vector);
  191. }
  192. U1 IREmitter::GetZeroFromOp(const Value& op) {
  193. return Inst<U1>(Opcode::GetZeroFromOp, op);
  194. }
  195. U1 IREmitter::GetSignFromOp(const Value& op) {
  196. return Inst<U1>(Opcode::GetSignFromOp, op);
  197. }
  198. U1 IREmitter::GetCarryFromOp(const Value& op) {
  199. return Inst<U1>(Opcode::GetCarryFromOp, op);
  200. }
  201. U1 IREmitter::GetOverflowFromOp(const Value& op) {
  202. return Inst<U1>(Opcode::GetOverflowFromOp, op);
  203. }
  204. F16F32F64 IREmitter::FPAdd(const F16F32F64& a, const F16F32F64& b, FpControl control) {
  205. if (a.Type() != a.Type()) {
  206. throw InvalidArgument("Mismatching types {} and {}", a.Type(), b.Type());
  207. }
  208. switch (a.Type()) {
  209. case Type::F16:
  210. return Inst<F16>(Opcode::FPAdd16, Flags{control}, a, b);
  211. case Type::F32:
  212. return Inst<F32>(Opcode::FPAdd32, Flags{control}, a, b);
  213. case Type::F64:
  214. return Inst<F64>(Opcode::FPAdd64, Flags{control}, a, b);
  215. default:
  216. ThrowInvalidType(a.Type());
  217. }
  218. }
  219. Value IREmitter::CompositeConstruct(const Value& e1, const Value& e2) {
  220. if (e1.Type() != e2.Type()) {
  221. throw InvalidArgument("Mismatching types {} and {}", e1.Type(), e2.Type());
  222. }
  223. switch (e1.Type()) {
  224. case Type::U32:
  225. return Inst(Opcode::CompositeConstructU32x2, e1, e2);
  226. case Type::F16:
  227. return Inst(Opcode::CompositeConstructF16x2, e1, e2);
  228. case Type::F32:
  229. return Inst(Opcode::CompositeConstructF32x2, e1, e2);
  230. case Type::F64:
  231. return Inst(Opcode::CompositeConstructF64x2, e1, e2);
  232. default:
  233. ThrowInvalidType(e1.Type());
  234. }
  235. }
  236. Value IREmitter::CompositeConstruct(const Value& e1, const Value& e2, const Value& e3) {
  237. if (e1.Type() != e2.Type() || e1.Type() != e3.Type()) {
  238. throw InvalidArgument("Mismatching types {}, {}, and {}", e1.Type(), e2.Type(), e3.Type());
  239. }
  240. switch (e1.Type()) {
  241. case Type::U32:
  242. return Inst(Opcode::CompositeConstructU32x3, e1, e2, e3);
  243. case Type::F16:
  244. return Inst(Opcode::CompositeConstructF16x3, e1, e2, e3);
  245. case Type::F32:
  246. return Inst(Opcode::CompositeConstructF32x3, e1, e2, e3);
  247. case Type::F64:
  248. return Inst(Opcode::CompositeConstructF64x3, e1, e2, e3);
  249. default:
  250. ThrowInvalidType(e1.Type());
  251. }
  252. }
  253. Value IREmitter::CompositeConstruct(const Value& e1, const Value& e2, const Value& e3,
  254. const Value& e4) {
  255. if (e1.Type() != e2.Type() || e1.Type() != e3.Type() || e1.Type() != e4.Type()) {
  256. throw InvalidArgument("Mismatching types {}, {}, {}, and {}", e1.Type(), e2.Type(),
  257. e3.Type(), e4.Type());
  258. }
  259. switch (e1.Type()) {
  260. case Type::U32:
  261. return Inst(Opcode::CompositeConstructU32x4, e1, e2, e3, e4);
  262. case Type::F16:
  263. return Inst(Opcode::CompositeConstructF16x4, e1, e2, e3, e4);
  264. case Type::F32:
  265. return Inst(Opcode::CompositeConstructF32x4, e1, e2, e3, e4);
  266. case Type::F64:
  267. return Inst(Opcode::CompositeConstructF64x4, e1, e2, e3, e4);
  268. default:
  269. ThrowInvalidType(e1.Type());
  270. }
  271. }
  272. Value IREmitter::CompositeExtract(const Value& vector, size_t element) {
  273. const auto read = [&](Opcode opcode, size_t limit) -> Value {
  274. if (element >= limit) {
  275. throw InvalidArgument("Out of bounds element {}", element);
  276. }
  277. return Inst(opcode, vector, Value{static_cast<u32>(element)});
  278. };
  279. switch (vector.Type()) {
  280. case Type::U32x2:
  281. return read(Opcode::CompositeExtractU32x2, 2);
  282. case Type::U32x3:
  283. return read(Opcode::CompositeExtractU32x3, 3);
  284. case Type::U32x4:
  285. return read(Opcode::CompositeExtractU32x4, 4);
  286. case Type::F16x2:
  287. return read(Opcode::CompositeExtractF16x2, 2);
  288. case Type::F16x3:
  289. return read(Opcode::CompositeExtractF16x3, 3);
  290. case Type::F16x4:
  291. return read(Opcode::CompositeExtractF16x4, 4);
  292. case Type::F32x2:
  293. return read(Opcode::CompositeExtractF32x2, 2);
  294. case Type::F32x3:
  295. return read(Opcode::CompositeExtractF32x3, 3);
  296. case Type::F32x4:
  297. return read(Opcode::CompositeExtractF32x4, 4);
  298. case Type::F64x2:
  299. return read(Opcode::CompositeExtractF64x2, 2);
  300. case Type::F64x3:
  301. return read(Opcode::CompositeExtractF64x3, 3);
  302. case Type::F64x4:
  303. return read(Opcode::CompositeExtractF64x4, 4);
  304. default:
  305. ThrowInvalidType(vector.Type());
  306. }
  307. }
  308. Value IREmitter::Select(const U1& condition, const Value& true_value, const Value& false_value) {
  309. if (true_value.Type() != false_value.Type()) {
  310. throw InvalidArgument("Mismatching types {} and {}", true_value.Type(), false_value.Type());
  311. }
  312. switch (true_value.Type()) {
  313. case Type::U8:
  314. return Inst(Opcode::SelectU8, condition, true_value, false_value);
  315. case Type::U16:
  316. return Inst(Opcode::SelectU16, condition, true_value, false_value);
  317. case Type::U32:
  318. return Inst(Opcode::SelectU32, condition, true_value, false_value);
  319. case Type::U64:
  320. return Inst(Opcode::SelectU64, condition, true_value, false_value);
  321. case Type::F32:
  322. return Inst(Opcode::SelectF32, condition, true_value, false_value);
  323. default:
  324. throw InvalidArgument("Invalid type {}", true_value.Type());
  325. }
  326. }
  327. template <>
  328. IR::U32 IREmitter::BitCast<IR::U32, IR::F32>(const IR::F32& value) {
  329. return Inst<IR::U32>(Opcode::BitCastU32F32, value);
  330. }
  331. template <>
  332. IR::F32 IREmitter::BitCast<IR::F32, IR::U32>(const IR::U32& value) {
  333. return Inst<IR::F32>(Opcode::BitCastF32U32, value);
  334. }
  335. template <>
  336. IR::U16 IREmitter::BitCast<IR::U16, IR::F16>(const IR::F16& value) {
  337. return Inst<IR::U16>(Opcode::BitCastU16F16, value);
  338. }
  339. template <>
  340. IR::F16 IREmitter::BitCast<IR::F16, IR::U16>(const IR::U16& value) {
  341. return Inst<IR::F16>(Opcode::BitCastF16U16, value);
  342. }
  343. template <>
  344. IR::U64 IREmitter::BitCast<IR::U64, IR::F64>(const IR::F64& value) {
  345. return Inst<IR::U64>(Opcode::BitCastU64F64, value);
  346. }
  347. template <>
  348. IR::F64 IREmitter::BitCast<IR::F64, IR::U64>(const IR::U64& value) {
  349. return Inst<IR::F64>(Opcode::BitCastF64U64, value);
  350. }
  351. U64 IREmitter::PackUint2x32(const Value& vector) {
  352. return Inst<U64>(Opcode::PackUint2x32, vector);
  353. }
  354. Value IREmitter::UnpackUint2x32(const U64& value) {
  355. return Inst<Value>(Opcode::UnpackUint2x32, value);
  356. }
  357. U32 IREmitter::PackFloat2x16(const Value& vector) {
  358. return Inst<U32>(Opcode::PackFloat2x16, vector);
  359. }
  360. Value IREmitter::UnpackFloat2x16(const U32& value) {
  361. return Inst<Value>(Opcode::UnpackFloat2x16, value);
  362. }
  363. F64 IREmitter::PackDouble2x32(const Value& vector) {
  364. return Inst<F64>(Opcode::PackDouble2x32, vector);
  365. }
  366. Value IREmitter::UnpackDouble2x32(const F64& value) {
  367. return Inst<Value>(Opcode::UnpackDouble2x32, value);
  368. }
  369. F16F32F64 IREmitter::FPMul(const F16F32F64& a, const F16F32F64& b, FpControl control) {
  370. if (a.Type() != b.Type()) {
  371. throw InvalidArgument("Mismatching types {} and {}", a.Type(), b.Type());
  372. }
  373. switch (a.Type()) {
  374. case Type::F16:
  375. return Inst<F16>(Opcode::FPMul16, Flags{control}, a, b);
  376. case Type::F32:
  377. return Inst<F32>(Opcode::FPMul32, Flags{control}, a, b);
  378. case Type::F64:
  379. return Inst<F64>(Opcode::FPMul64, Flags{control}, a, b);
  380. default:
  381. ThrowInvalidType(a.Type());
  382. }
  383. }
  384. F16F32F64 IREmitter::FPFma(const F16F32F64& a, const F16F32F64& b, const F16F32F64& c,
  385. FpControl control) {
  386. if (a.Type() != b.Type() || a.Type() != c.Type()) {
  387. throw InvalidArgument("Mismatching types {}, {}, and {}", a.Type(), b.Type(), c.Type());
  388. }
  389. switch (a.Type()) {
  390. case Type::F16:
  391. return Inst<F16>(Opcode::FPFma16, Flags{control}, a, b, c);
  392. case Type::F32:
  393. return Inst<F32>(Opcode::FPFma32, Flags{control}, a, b, c);
  394. case Type::F64:
  395. return Inst<F64>(Opcode::FPFma64, Flags{control}, a, b, c);
  396. default:
  397. ThrowInvalidType(a.Type());
  398. }
  399. }
  400. F16F32F64 IREmitter::FPAbs(const F16F32F64& value) {
  401. switch (value.Type()) {
  402. case Type::F16:
  403. return Inst<F16>(Opcode::FPAbs16, value);
  404. case Type::F32:
  405. return Inst<F32>(Opcode::FPAbs32, value);
  406. case Type::F64:
  407. return Inst<F64>(Opcode::FPAbs64, value);
  408. default:
  409. ThrowInvalidType(value.Type());
  410. }
  411. }
  412. F16F32F64 IREmitter::FPNeg(const F16F32F64& value) {
  413. switch (value.Type()) {
  414. case Type::F16:
  415. return Inst<F16>(Opcode::FPNeg16, value);
  416. case Type::F32:
  417. return Inst<F32>(Opcode::FPNeg32, value);
  418. case Type::F64:
  419. return Inst<F64>(Opcode::FPNeg64, value);
  420. default:
  421. ThrowInvalidType(value.Type());
  422. }
  423. }
  424. F16F32F64 IREmitter::FPAbsNeg(const F16F32F64& value, bool abs, bool neg) {
  425. F16F32F64 result{value};
  426. if (abs) {
  427. result = FPAbs(result);
  428. }
  429. if (neg) {
  430. result = FPNeg(result);
  431. }
  432. return result;
  433. }
  434. F32 IREmitter::FPCos(const F32& value) {
  435. return Inst<F32>(Opcode::FPCos, value);
  436. }
  437. F32 IREmitter::FPSin(const F32& value) {
  438. return Inst<F32>(Opcode::FPSin, value);
  439. }
  440. F32 IREmitter::FPExp2(const F32& value) {
  441. return Inst<F32>(Opcode::FPExp2, value);
  442. }
  443. F32 IREmitter::FPLog2(const F32& value) {
  444. return Inst<F32>(Opcode::FPLog2, value);
  445. }
  446. F32F64 IREmitter::FPRecip(const F32F64& value) {
  447. switch (value.Type()) {
  448. case Type::F32:
  449. return Inst<F32>(Opcode::FPRecip32, value);
  450. case Type::F64:
  451. return Inst<F64>(Opcode::FPRecip64, value);
  452. default:
  453. ThrowInvalidType(value.Type());
  454. }
  455. }
  456. F32F64 IREmitter::FPRecipSqrt(const F32F64& value) {
  457. switch (value.Type()) {
  458. case Type::F32:
  459. return Inst<F32>(Opcode::FPRecipSqrt32, value);
  460. case Type::F64:
  461. return Inst<F64>(Opcode::FPRecipSqrt64, value);
  462. default:
  463. ThrowInvalidType(value.Type());
  464. }
  465. }
  466. F32 IREmitter::FPSqrt(const F32& value) {
  467. return Inst<F32>(Opcode::FPSqrt, value);
  468. }
  469. F16F32F64 IREmitter::FPSaturate(const F16F32F64& value) {
  470. switch (value.Type()) {
  471. case Type::F16:
  472. return Inst<F16>(Opcode::FPSaturate16, value);
  473. case Type::F32:
  474. return Inst<F32>(Opcode::FPSaturate32, value);
  475. case Type::F64:
  476. return Inst<F64>(Opcode::FPSaturate64, value);
  477. default:
  478. ThrowInvalidType(value.Type());
  479. }
  480. }
  481. F16F32F64 IREmitter::FPRoundEven(const F16F32F64& value, FpControl control) {
  482. switch (value.Type()) {
  483. case Type::F16:
  484. return Inst<F16>(Opcode::FPRoundEven16, Flags{control}, value);
  485. case Type::F32:
  486. return Inst<F32>(Opcode::FPRoundEven32, Flags{control}, value);
  487. case Type::F64:
  488. return Inst<F64>(Opcode::FPRoundEven64, Flags{control}, value);
  489. default:
  490. ThrowInvalidType(value.Type());
  491. }
  492. }
  493. F16F32F64 IREmitter::FPFloor(const F16F32F64& value, FpControl control) {
  494. switch (value.Type()) {
  495. case Type::F16:
  496. return Inst<F16>(Opcode::FPFloor16, Flags{control}, value);
  497. case Type::F32:
  498. return Inst<F32>(Opcode::FPFloor32, Flags{control}, value);
  499. case Type::F64:
  500. return Inst<F64>(Opcode::FPFloor64, Flags{control}, value);
  501. default:
  502. ThrowInvalidType(value.Type());
  503. }
  504. }
  505. F16F32F64 IREmitter::FPCeil(const F16F32F64& value, FpControl control) {
  506. switch (value.Type()) {
  507. case Type::F16:
  508. return Inst<F16>(Opcode::FPCeil16, Flags{control}, value);
  509. case Type::F32:
  510. return Inst<F32>(Opcode::FPCeil32, Flags{control}, value);
  511. case Type::F64:
  512. return Inst<F64>(Opcode::FPCeil64, Flags{control}, value);
  513. default:
  514. ThrowInvalidType(value.Type());
  515. }
  516. }
  517. F16F32F64 IREmitter::FPTrunc(const F16F32F64& value, FpControl control) {
  518. switch (value.Type()) {
  519. case Type::F16:
  520. return Inst<F16>(Opcode::FPTrunc16, Flags{control}, value);
  521. case Type::F32:
  522. return Inst<F32>(Opcode::FPTrunc32, Flags{control}, value);
  523. case Type::F64:
  524. return Inst<F64>(Opcode::FPTrunc64, Flags{control}, value);
  525. default:
  526. ThrowInvalidType(value.Type());
  527. }
  528. }
  529. U1 IREmitter::FPEqual(const F16F32F64& lhs, const F16F32F64& rhs, bool ordered) {
  530. if (lhs.Type() != rhs.Type()) {
  531. throw InvalidArgument("Mismatching types {} and {}", lhs.Type(), rhs.Type());
  532. }
  533. switch (lhs.Type()) {
  534. case Type::F16:
  535. return Inst<U1>(ordered ? Opcode::FPOrdEqual16 : Opcode::FPUnordEqual16, lhs, rhs);
  536. case Type::F32:
  537. return Inst<U1>(ordered ? Opcode::FPOrdEqual32 : Opcode::FPUnordEqual32, lhs, rhs);
  538. case Type::F64:
  539. return Inst<U1>(ordered ? Opcode::FPOrdEqual64 : Opcode::FPUnordEqual64, lhs, rhs);
  540. default:
  541. ThrowInvalidType(lhs.Type());
  542. }
  543. }
  544. U1 IREmitter::FPNotEqual(const F16F32F64& lhs, const F16F32F64& rhs, bool ordered) {
  545. if (lhs.Type() != rhs.Type()) {
  546. throw InvalidArgument("Mismatching types {} and {}", lhs.Type(), rhs.Type());
  547. }
  548. switch (lhs.Type()) {
  549. case Type::F16:
  550. return Inst<U1>(ordered ? Opcode::FPOrdNotEqual16 : Opcode::FPUnordNotEqual16, lhs, rhs);
  551. case Type::F32:
  552. return Inst<U1>(ordered ? Opcode::FPOrdNotEqual32 : Opcode::FPUnordNotEqual32, lhs, rhs);
  553. case Type::F64:
  554. return Inst<U1>(ordered ? Opcode::FPOrdNotEqual64 : Opcode::FPUnordNotEqual64, lhs, rhs);
  555. default:
  556. ThrowInvalidType(lhs.Type());
  557. }
  558. }
  559. U1 IREmitter::FPLessThan(const F16F32F64& lhs, const F16F32F64& rhs, bool ordered) {
  560. if (lhs.Type() != rhs.Type()) {
  561. throw InvalidArgument("Mismatching types {} and {}", lhs.Type(), rhs.Type());
  562. }
  563. switch (lhs.Type()) {
  564. case Type::F16:
  565. return Inst<U1>(ordered ? Opcode::FPOrdLessThan16 : Opcode::FPUnordLessThan16, lhs, rhs);
  566. case Type::F32:
  567. return Inst<U1>(ordered ? Opcode::FPOrdLessThan32 : Opcode::FPUnordLessThan32, lhs, rhs);
  568. case Type::F64:
  569. return Inst<U1>(ordered ? Opcode::FPOrdLessThan64 : Opcode::FPUnordLessThan64, lhs, rhs);
  570. default:
  571. ThrowInvalidType(lhs.Type());
  572. }
  573. }
  574. U1 IREmitter::FPGreaterThan(const F16F32F64& lhs, const F16F32F64& rhs, bool ordered) {
  575. if (lhs.Type() != rhs.Type()) {
  576. throw InvalidArgument("Mismatching types {} and {}", lhs.Type(), rhs.Type());
  577. }
  578. switch (lhs.Type()) {
  579. case Type::F16:
  580. return Inst<U1>(ordered ? Opcode::FPOrdGreaterThan16 : Opcode::FPUnordGreaterThan16, lhs,
  581. rhs);
  582. case Type::F32:
  583. return Inst<U1>(ordered ? Opcode::FPOrdGreaterThan32 : Opcode::FPUnordGreaterThan32, lhs,
  584. rhs);
  585. case Type::F64:
  586. return Inst<U1>(ordered ? Opcode::FPOrdGreaterThan64 : Opcode::FPUnordGreaterThan64, lhs,
  587. rhs);
  588. default:
  589. ThrowInvalidType(lhs.Type());
  590. }
  591. }
  592. U1 IREmitter::FPLessThanEqual(const F16F32F64& lhs, const F16F32F64& rhs, bool ordered) {
  593. if (lhs.Type() != rhs.Type()) {
  594. throw InvalidArgument("Mismatching types {} and {}", lhs.Type(), rhs.Type());
  595. }
  596. switch (lhs.Type()) {
  597. case Type::F16:
  598. return Inst<U1>(ordered ? Opcode::FPOrdLessThanEqual16 : Opcode::FPUnordLessThanEqual16,
  599. lhs, rhs);
  600. case Type::F32:
  601. return Inst<U1>(ordered ? Opcode::FPOrdLessThanEqual32 : Opcode::FPUnordLessThanEqual32,
  602. lhs, rhs);
  603. case Type::F64:
  604. return Inst<U1>(ordered ? Opcode::FPOrdLessThanEqual64 : Opcode::FPUnordLessThanEqual64,
  605. lhs, rhs);
  606. default:
  607. ThrowInvalidType(lhs.Type());
  608. }
  609. }
  610. U1 IREmitter::FPGreaterThanEqual(const F16F32F64& lhs, const F16F32F64& rhs, bool ordered) {
  611. if (lhs.Type() != rhs.Type()) {
  612. throw InvalidArgument("Mismatching types {} and {}", lhs.Type(), rhs.Type());
  613. }
  614. switch (lhs.Type()) {
  615. case Type::F16:
  616. return Inst<U1>(ordered ? Opcode::FPOrdGreaterThanEqual16
  617. : Opcode::FPUnordGreaterThanEqual16,
  618. lhs, rhs);
  619. case Type::F32:
  620. return Inst<U1>(ordered ? Opcode::FPOrdGreaterThanEqual32
  621. : Opcode::FPUnordGreaterThanEqual32,
  622. lhs, rhs);
  623. case Type::F64:
  624. return Inst<U1>(ordered ? Opcode::FPOrdGreaterThanEqual64
  625. : Opcode::FPUnordGreaterThanEqual64,
  626. lhs, rhs);
  627. default:
  628. ThrowInvalidType(lhs.Type());
  629. }
  630. }
  631. U32U64 IREmitter::IAdd(const U32U64& a, const U32U64& b) {
  632. if (a.Type() != b.Type()) {
  633. throw InvalidArgument("Mismatching types {} and {}", a.Type(), b.Type());
  634. }
  635. switch (a.Type()) {
  636. case Type::U32:
  637. return Inst<U32>(Opcode::IAdd32, a, b);
  638. case Type::U64:
  639. return Inst<U64>(Opcode::IAdd64, a, b);
  640. default:
  641. ThrowInvalidType(a.Type());
  642. }
  643. }
  644. U32U64 IREmitter::ISub(const U32U64& a, const U32U64& b) {
  645. if (a.Type() != b.Type()) {
  646. throw InvalidArgument("Mismatching types {} and {}", a.Type(), b.Type());
  647. }
  648. switch (a.Type()) {
  649. case Type::U32:
  650. return Inst<U32>(Opcode::ISub32, a, b);
  651. case Type::U64:
  652. return Inst<U64>(Opcode::ISub64, a, b);
  653. default:
  654. ThrowInvalidType(a.Type());
  655. }
  656. }
  657. U32 IREmitter::IMul(const U32& a, const U32& b) {
  658. return Inst<U32>(Opcode::IMul32, a, b);
  659. }
  660. U32 IREmitter::INeg(const U32& value) {
  661. return Inst<U32>(Opcode::INeg32, value);
  662. }
  663. U32 IREmitter::IAbs(const U32& value) {
  664. return Inst<U32>(Opcode::IAbs32, value);
  665. }
  666. U32 IREmitter::ShiftLeftLogical(const U32& base, const U32& shift) {
  667. return Inst<U32>(Opcode::ShiftLeftLogical32, base, shift);
  668. }
  669. U32 IREmitter::ShiftRightLogical(const U32& base, const U32& shift) {
  670. return Inst<U32>(Opcode::ShiftRightLogical32, base, shift);
  671. }
  672. U32 IREmitter::ShiftRightArithmetic(const U32& base, const U32& shift) {
  673. return Inst<U32>(Opcode::ShiftRightArithmetic32, base, shift);
  674. }
  675. U32 IREmitter::BitwiseAnd(const U32& a, const U32& b) {
  676. return Inst<U32>(Opcode::BitwiseAnd32, a, b);
  677. }
  678. U32 IREmitter::BitwiseOr(const U32& a, const U32& b) {
  679. return Inst<U32>(Opcode::BitwiseOr32, a, b);
  680. }
  681. U32 IREmitter::BitwiseXor(const U32& a, const U32& b) {
  682. return Inst<U32>(Opcode::BitwiseXor32, a, b);
  683. }
  684. U32 IREmitter::BitFieldInsert(const U32& base, const U32& insert, const U32& offset,
  685. const U32& count) {
  686. return Inst<U32>(Opcode::BitFieldInsert, base, insert, offset, count);
  687. }
  688. U32 IREmitter::BitFieldExtract(const U32& base, const U32& offset, const U32& count,
  689. bool is_signed) {
  690. return Inst<U32>(is_signed ? Opcode::BitFieldSExtract : Opcode::BitFieldUExtract, base, offset,
  691. count);
  692. }
  693. U32 IREmitter::BitReverse(const U32& value) {
  694. return Inst<U32>(Opcode::BitReverse32, value);
  695. }
  696. U32 IREmitter::BitCount(const U32& value) {
  697. return Inst<U32>(Opcode::BitCount32, value);
  698. }
  699. U32 IREmitter::BitwiseNot(const U32& value) {
  700. return Inst<U32>(Opcode::BitwiseNot32, value);
  701. }
  702. U32 IREmitter::FindSMsb(const U32& value) {
  703. return Inst<U32>(Opcode::FindSMsb32, value);
  704. }
  705. U32 IREmitter::FindUMsb(const U32& value) {
  706. return Inst<U32>(Opcode::FindUMsb32, value);
  707. }
  708. U32 IREmitter::SMin(const U32& a, const U32& b) {
  709. return Inst<U32>(Opcode::SMin32, a, b);
  710. }
  711. U32 IREmitter::UMin(const U32& a, const U32& b) {
  712. return Inst<U32>(Opcode::UMin32, a, b);
  713. }
  714. U32 IREmitter::SMax(const U32& a, const U32& b) {
  715. return Inst<U32>(Opcode::SMax32, a, b);
  716. }
  717. U32 IREmitter::UMax(const U32& a, const U32& b) {
  718. return Inst<U32>(Opcode::UMax32, a, b);
  719. }
  720. U1 IREmitter::ILessThan(const U32& lhs, const U32& rhs, bool is_signed) {
  721. return Inst<U1>(is_signed ? Opcode::SLessThan : Opcode::ULessThan, lhs, rhs);
  722. }
  723. U1 IREmitter::IEqual(const U32& lhs, const U32& rhs) {
  724. return Inst<U1>(Opcode::IEqual, lhs, rhs);
  725. }
  726. U1 IREmitter::ILessThanEqual(const U32& lhs, const U32& rhs, bool is_signed) {
  727. return Inst<U1>(is_signed ? Opcode::SLessThanEqual : Opcode::ULessThanEqual, lhs, rhs);
  728. }
  729. U1 IREmitter::IGreaterThan(const U32& lhs, const U32& rhs, bool is_signed) {
  730. return Inst<U1>(is_signed ? Opcode::SGreaterThan : Opcode::UGreaterThan, lhs, rhs);
  731. }
  732. U1 IREmitter::INotEqual(const U32& lhs, const U32& rhs) {
  733. return Inst<U1>(Opcode::INotEqual, lhs, rhs);
  734. }
  735. U1 IREmitter::IGreaterThanEqual(const U32& lhs, const U32& rhs, bool is_signed) {
  736. return Inst<U1>(is_signed ? Opcode::SGreaterThanEqual : Opcode::UGreaterThanEqual, lhs, rhs);
  737. }
  738. U1 IREmitter::LogicalOr(const U1& a, const U1& b) {
  739. return Inst<U1>(Opcode::LogicalOr, a, b);
  740. }
  741. U1 IREmitter::LogicalAnd(const U1& a, const U1& b) {
  742. return Inst<U1>(Opcode::LogicalAnd, a, b);
  743. }
  744. U1 IREmitter::LogicalXor(const U1& a, const U1& b) {
  745. return Inst<U1>(Opcode::LogicalXor, a, b);
  746. }
  747. U1 IREmitter::LogicalNot(const U1& value) {
  748. return Inst<U1>(Opcode::LogicalNot, value);
  749. }
  750. U32U64 IREmitter::ConvertFToS(size_t bitsize, const F16F32F64& value) {
  751. switch (bitsize) {
  752. case 16:
  753. switch (value.Type()) {
  754. case Type::F16:
  755. return Inst<U32>(Opcode::ConvertS16F16, value);
  756. case Type::F32:
  757. return Inst<U32>(Opcode::ConvertS16F32, value);
  758. case Type::F64:
  759. return Inst<U32>(Opcode::ConvertS16F64, value);
  760. default:
  761. ThrowInvalidType(value.Type());
  762. }
  763. case 32:
  764. switch (value.Type()) {
  765. case Type::F16:
  766. return Inst<U32>(Opcode::ConvertS32F16, value);
  767. case Type::F32:
  768. return Inst<U32>(Opcode::ConvertS32F32, value);
  769. case Type::F64:
  770. return Inst<U32>(Opcode::ConvertS32F64, value);
  771. default:
  772. ThrowInvalidType(value.Type());
  773. }
  774. case 64:
  775. switch (value.Type()) {
  776. case Type::F16:
  777. return Inst<U64>(Opcode::ConvertS64F16, value);
  778. case Type::F32:
  779. return Inst<U64>(Opcode::ConvertS64F32, value);
  780. case Type::F64:
  781. return Inst<U64>(Opcode::ConvertS64F64, value);
  782. default:
  783. ThrowInvalidType(value.Type());
  784. }
  785. default:
  786. throw InvalidArgument("Invalid destination bitsize {}", bitsize);
  787. }
  788. }
  789. U32U64 IREmitter::ConvertFToU(size_t bitsize, const F16F32F64& value) {
  790. switch (bitsize) {
  791. case 16:
  792. switch (value.Type()) {
  793. case Type::F16:
  794. return Inst<U32>(Opcode::ConvertU16F16, value);
  795. case Type::F32:
  796. return Inst<U32>(Opcode::ConvertU16F32, value);
  797. case Type::F64:
  798. return Inst<U32>(Opcode::ConvertU16F64, value);
  799. default:
  800. ThrowInvalidType(value.Type());
  801. }
  802. case 32:
  803. switch (value.Type()) {
  804. case Type::F16:
  805. return Inst<U32>(Opcode::ConvertU32F16, value);
  806. case Type::F32:
  807. return Inst<U32>(Opcode::ConvertU32F32, value);
  808. case Type::F64:
  809. return Inst<U32>(Opcode::ConvertU32F64, value);
  810. default:
  811. ThrowInvalidType(value.Type());
  812. }
  813. case 64:
  814. switch (value.Type()) {
  815. case Type::F16:
  816. return Inst<U64>(Opcode::ConvertU64F16, value);
  817. case Type::F32:
  818. return Inst<U64>(Opcode::ConvertU64F32, value);
  819. case Type::F64:
  820. return Inst<U64>(Opcode::ConvertU64F64, value);
  821. default:
  822. ThrowInvalidType(value.Type());
  823. }
  824. default:
  825. throw InvalidArgument("Invalid destination bitsize {}", bitsize);
  826. }
  827. }
  828. U32U64 IREmitter::ConvertFToI(size_t bitsize, bool is_signed, const F16F32F64& value) {
  829. if (is_signed) {
  830. return ConvertFToS(bitsize, value);
  831. } else {
  832. return ConvertFToU(bitsize, value);
  833. }
  834. }
  835. U32U64 IREmitter::ConvertU(size_t result_bitsize, const U32U64& value) {
  836. switch (result_bitsize) {
  837. case 32:
  838. switch (value.Type()) {
  839. case Type::U32:
  840. // Nothing to do
  841. return value;
  842. case Type::U64:
  843. return Inst<U32>(Opcode::ConvertU32U64, value);
  844. default:
  845. break;
  846. }
  847. break;
  848. case 64:
  849. switch (value.Type()) {
  850. case Type::U32:
  851. return Inst<U64>(Opcode::ConvertU64U32, value);
  852. case Type::U64:
  853. // Nothing to do
  854. return value;
  855. default:
  856. break;
  857. }
  858. }
  859. throw NotImplementedException("Conversion from {} to {} bits", value.Type(), result_bitsize);
  860. }
  861. } // namespace Shader::IR