handle_call.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include <algorithm>
  5. #include <vector>
  6. #include "common/raw_string_ostream.h"
  7. #include "llvm/IR/Type.h"
  8. #include "llvm/IR/Value.h"
  9. #include "toolchain/lower/function_context.h"
  10. #include "toolchain/sem_ir/builtin_function_kind.h"
  11. #include "toolchain/sem_ir/function.h"
  12. #include "toolchain/sem_ir/ids.h"
  13. #include "toolchain/sem_ir/typed_insts.h"
  14. namespace Carbon::Lower {
  15. // Get the predicate to use for an `icmp` instruction generated for the
  16. // specified builtin.
  17. static auto GetBuiltinICmpPredicate(SemIR::BuiltinFunctionKind builtin_kind,
  18. bool is_signed)
  19. -> llvm::CmpInst::Predicate {
  20. switch (builtin_kind) {
  21. case SemIR::BuiltinFunctionKind::IntEq:
  22. case SemIR::BuiltinFunctionKind::BoolEq:
  23. return llvm::CmpInst::ICMP_EQ;
  24. case SemIR::BuiltinFunctionKind::IntNeq:
  25. case SemIR::BuiltinFunctionKind::BoolNeq:
  26. return llvm::CmpInst::ICMP_NE;
  27. case SemIR::BuiltinFunctionKind::IntLess:
  28. return is_signed ? llvm::CmpInst::ICMP_SLT : llvm::CmpInst::ICMP_ULT;
  29. case SemIR::BuiltinFunctionKind::IntLessEq:
  30. return is_signed ? llvm::CmpInst::ICMP_SLE : llvm::CmpInst::ICMP_ULE;
  31. case SemIR::BuiltinFunctionKind::IntGreater:
  32. return is_signed ? llvm::CmpInst::ICMP_SGT : llvm::CmpInst::ICMP_UGT;
  33. case SemIR::BuiltinFunctionKind::IntGreaterEq:
  34. return is_signed ? llvm::CmpInst::ICMP_SGE : llvm::CmpInst::ICMP_UGE;
  35. default:
  36. CARBON_FATAL("Unexpected builtin kind {0}", builtin_kind);
  37. }
  38. }
  39. // Get the predicate to use for an `fcmp` instruction generated for the
  40. // specified builtin.
  41. static auto GetBuiltinFCmpPredicate(SemIR::BuiltinFunctionKind builtin_kind)
  42. -> llvm::CmpInst::Predicate {
  43. switch (builtin_kind) {
  44. case SemIR::BuiltinFunctionKind::FloatEq:
  45. return llvm::CmpInst::FCMP_OEQ;
  46. case SemIR::BuiltinFunctionKind::FloatNeq:
  47. return llvm::CmpInst::FCMP_ONE;
  48. case SemIR::BuiltinFunctionKind::FloatLess:
  49. return llvm::CmpInst::FCMP_OLT;
  50. case SemIR::BuiltinFunctionKind::FloatLessEq:
  51. return llvm::CmpInst::FCMP_OLE;
  52. case SemIR::BuiltinFunctionKind::FloatGreater:
  53. return llvm::CmpInst::FCMP_OGT;
  54. case SemIR::BuiltinFunctionKind::FloatGreaterEq:
  55. return llvm::CmpInst::FCMP_OGE;
  56. default:
  57. CARBON_FATAL("Unexpected builtin kind {0}", builtin_kind);
  58. }
  59. }
  60. // Returns whether the specified instruction has a signed integer type.
  61. static auto IsSignedInt(FunctionContext& context, SemIR::InstId int_id)
  62. -> bool {
  63. auto [file, type_id] = context.GetTypeIdOfInst(int_id);
  64. return file->types().IsSignedInt(type_id);
  65. }
  66. // Creates a zext or sext instruction depending on the signedness of the
  67. // operand.
  68. static auto CreateExt(FunctionContext& context, llvm::Value* value,
  69. llvm::Type* type, bool is_signed,
  70. const llvm::Twine& name = "") -> llvm::Value* {
  71. return is_signed ? context.builder().CreateSExt(value, type, name)
  72. : context.builder().CreateZExt(value, type, name);
  73. }
  74. // Creates a zext, sext, or trunc instruction depending on the signedness of the
  75. // operand.
  76. static auto CreateExtOrTrunc(FunctionContext& context, llvm::Value* value,
  77. llvm::Type* type, bool is_signed,
  78. const llvm::Twine& name = "") -> llvm::Value* {
  79. return is_signed ? context.builder().CreateSExtOrTrunc(value, type, name)
  80. : context.builder().CreateZExtOrTrunc(value, type, name);
  81. }
  82. // Create a integer bit shift for a call to a builtin bit shift function.
  83. static auto CreateIntShift(FunctionContext& context,
  84. llvm::Instruction::BinaryOps bin_op,
  85. llvm::Value* lhs, llvm::Value* rhs) -> llvm::Value* {
  86. // Weirdly, LLVM requires the operands of bit shift operators to be of the
  87. // same type. We can always use the width of the LHS, because if the RHS
  88. // doesn't fit in that then the cast is out of range anyway. Zero-extending is
  89. // always fine because it's an error for the RHS to be negative.
  90. //
  91. // TODO: In a development build we should trap if the RHS is signed and
  92. // negative or greater than or equal to the number of bits in the left-hand
  93. // type.
  94. rhs = context.builder().CreateZExtOrTrunc(rhs, lhs->getType(), "rhs");
  95. return context.builder().CreateBinOp(bin_op, lhs, rhs);
  96. }
  97. // Handles a call to a builtin integer comparison operator.
  98. static auto HandleIntComparison(FunctionContext& context, SemIR::InstId inst_id,
  99. SemIR::BuiltinFunctionKind builtin_kind,
  100. SemIR::InstId lhs_id, SemIR::InstId rhs_id)
  101. -> void {
  102. llvm::Value* lhs = context.GetValue(lhs_id);
  103. llvm::Value* rhs = context.GetValue(rhs_id);
  104. const auto* lhs_type = cast<llvm::IntegerType>(lhs->getType());
  105. const auto* rhs_type = cast<llvm::IntegerType>(rhs->getType());
  106. // We perform a signed comparison if either operand is signed.
  107. bool lhs_signed = IsSignedInt(context, lhs_id);
  108. bool rhs_signed = IsSignedInt(context, rhs_id);
  109. bool cmp_signed = lhs_signed || rhs_signed;
  110. // Compute the width for the comparison. This is the smallest width that
  111. // fits both types, after widening them to include a sign bit if
  112. // necessary.
  113. auto width_for_cmp = [&](const llvm::IntegerType* type, bool is_signed) {
  114. unsigned width = type->getBitWidth();
  115. if (!is_signed && cmp_signed) {
  116. // We're performing a signed comparison but this input is unsigned.
  117. // Widen it by at least one bit to provide a sign bit.
  118. ++width;
  119. }
  120. return width;
  121. };
  122. // TODO: This might be an awkward size, such as 33 or 65 bits, for a
  123. // signed/unsigned comparison. Would it be better to round this up to a
  124. // "nicer" bit width?
  125. unsigned cmp_width = std::max(width_for_cmp(lhs_type, lhs_signed),
  126. width_for_cmp(rhs_type, rhs_signed));
  127. auto* cmp_type = llvm::IntegerType::get(context.llvm_context(), cmp_width);
  128. // Widen the operands as needed.
  129. lhs = CreateExt(context, lhs, cmp_type, lhs_signed, "lhs");
  130. rhs = CreateExt(context, rhs, cmp_type, rhs_signed, "rhs");
  131. context.SetLocal(
  132. inst_id,
  133. context.builder().CreateICmp(
  134. GetBuiltinICmpPredicate(builtin_kind, cmp_signed), lhs, rhs));
  135. }
  136. // Creates a binary operator for a call to a builtin for either that operation
  137. // or the corresponding compound assignment.
  138. static auto CreateBinaryOperatorForBuiltin(
  139. FunctionContext& context, SemIR::InstId inst_id,
  140. SemIR::BuiltinFunctionKind builtin_kind, llvm::Value* lhs, llvm::Value* rhs)
  141. -> llvm::Value* {
  142. // TODO: Consider setting this to true in the performance build mode if the
  143. // result type is a signed integer type.
  144. constexpr bool SignedOverflowIsUB = false;
  145. switch (builtin_kind) {
  146. case SemIR::BuiltinFunctionKind::IntSAdd:
  147. case SemIR::BuiltinFunctionKind::IntSAddAssign: {
  148. return context.builder().CreateAdd(lhs, rhs, "",
  149. /*HasNUW=*/false,
  150. /*HasNSW=*/SignedOverflowIsUB);
  151. }
  152. case SemIR::BuiltinFunctionKind::IntSSub:
  153. case SemIR::BuiltinFunctionKind::IntSSubAssign: {
  154. return context.builder().CreateSub(lhs, rhs, "",
  155. /*HasNUW=*/false,
  156. /*HasNSW=*/SignedOverflowIsUB);
  157. }
  158. case SemIR::BuiltinFunctionKind::IntSMul:
  159. case SemIR::BuiltinFunctionKind::IntSMulAssign: {
  160. return context.builder().CreateMul(lhs, rhs, "",
  161. /*HasNUW=*/false,
  162. /*HasNSW=*/SignedOverflowIsUB);
  163. }
  164. case SemIR::BuiltinFunctionKind::IntSDiv:
  165. case SemIR::BuiltinFunctionKind::IntSDivAssign: {
  166. return context.builder().CreateSDiv(lhs, rhs);
  167. }
  168. case SemIR::BuiltinFunctionKind::IntSMod:
  169. case SemIR::BuiltinFunctionKind::IntSModAssign: {
  170. return context.builder().CreateSRem(lhs, rhs);
  171. }
  172. case SemIR::BuiltinFunctionKind::IntUAdd:
  173. case SemIR::BuiltinFunctionKind::IntUAddAssign: {
  174. return context.builder().CreateAdd(lhs, rhs);
  175. }
  176. case SemIR::BuiltinFunctionKind::IntUSub:
  177. case SemIR::BuiltinFunctionKind::IntUSubAssign: {
  178. return context.builder().CreateSub(lhs, rhs);
  179. }
  180. case SemIR::BuiltinFunctionKind::IntUMul:
  181. case SemIR::BuiltinFunctionKind::IntUMulAssign: {
  182. return context.builder().CreateMul(lhs, rhs);
  183. }
  184. case SemIR::BuiltinFunctionKind::IntUDiv:
  185. case SemIR::BuiltinFunctionKind::IntUDivAssign: {
  186. return context.builder().CreateUDiv(lhs, rhs);
  187. }
  188. case SemIR::BuiltinFunctionKind::IntUMod:
  189. case SemIR::BuiltinFunctionKind::IntUModAssign: {
  190. return context.builder().CreateURem(lhs, rhs);
  191. }
  192. case SemIR::BuiltinFunctionKind::IntAnd:
  193. case SemIR::BuiltinFunctionKind::IntAndAssign: {
  194. return context.builder().CreateAnd(lhs, rhs);
  195. }
  196. case SemIR::BuiltinFunctionKind::IntOr:
  197. case SemIR::BuiltinFunctionKind::IntOrAssign: {
  198. return context.builder().CreateOr(lhs, rhs);
  199. }
  200. case SemIR::BuiltinFunctionKind::IntXor:
  201. case SemIR::BuiltinFunctionKind::IntXorAssign: {
  202. return context.builder().CreateXor(lhs, rhs);
  203. }
  204. case SemIR::BuiltinFunctionKind::IntLeftShift:
  205. case SemIR::BuiltinFunctionKind::IntLeftShiftAssign: {
  206. return CreateIntShift(context, llvm::Instruction::Shl, lhs, rhs);
  207. }
  208. case SemIR::BuiltinFunctionKind::IntRightShift:
  209. case SemIR::BuiltinFunctionKind::IntRightShiftAssign: {
  210. // TODO: Split each of these builtins into separate signed and unsigned
  211. // builtins rather than working out here whether we're performing an
  212. // arithmetic or logical shift.
  213. auto lhs_id = context.sem_ir().inst_blocks().Get(
  214. context.sem_ir().insts().GetAs<SemIR::Call>(inst_id).args_id)[0];
  215. auto [lhs_type_file, lhs_type_id] = context.GetTypeIdOfInst(lhs_id);
  216. if (builtin_kind == SemIR::BuiltinFunctionKind::IntRightShiftAssign) {
  217. lhs_type_id = lhs_type_file->GetPointeeType(lhs_type_id);
  218. }
  219. return CreateIntShift(context,
  220. lhs_type_file->types().IsSignedInt(lhs_type_id)
  221. ? llvm::Instruction::AShr
  222. : llvm::Instruction::LShr,
  223. lhs, rhs);
  224. }
  225. case SemIR::BuiltinFunctionKind::FloatAdd:
  226. case SemIR::BuiltinFunctionKind::FloatAddAssign: {
  227. return context.builder().CreateFAdd(lhs, rhs);
  228. }
  229. case SemIR::BuiltinFunctionKind::FloatSub:
  230. case SemIR::BuiltinFunctionKind::FloatSubAssign: {
  231. return context.builder().CreateFSub(lhs, rhs);
  232. }
  233. case SemIR::BuiltinFunctionKind::FloatMul:
  234. case SemIR::BuiltinFunctionKind::FloatMulAssign: {
  235. return context.builder().CreateFMul(lhs, rhs);
  236. }
  237. case SemIR::BuiltinFunctionKind::FloatDiv:
  238. case SemIR::BuiltinFunctionKind::FloatDivAssign: {
  239. return context.builder().CreateFDiv(lhs, rhs);
  240. }
  241. default: {
  242. CARBON_FATAL("Unexpected binary operator {0}", builtin_kind);
  243. }
  244. }
  245. }
  246. // Handles a call to a builtin function.
  247. static auto HandleBuiltinCall(FunctionContext& context, SemIR::InstId inst_id,
  248. SemIR::BuiltinFunctionKind builtin_kind,
  249. llvm::ArrayRef<SemIR::InstId> arg_ids) -> void {
  250. // TODO: Consider setting this to true in the performance build mode if the
  251. // result type is a signed integer type.
  252. constexpr bool SignedOverflowIsUB = false;
  253. // TODO: Move the instruction names here into InstNamer.
  254. switch (builtin_kind) {
  255. case SemIR::BuiltinFunctionKind::None:
  256. CARBON_FATAL("No callee in function call.");
  257. case SemIR::BuiltinFunctionKind::NoOp:
  258. return;
  259. case SemIR::BuiltinFunctionKind::PrimitiveCopy:
  260. context.SetLocal(inst_id, context.GetValue(arg_ids[0]));
  261. return;
  262. case SemIR::BuiltinFunctionKind::PrintChar: {
  263. auto* i32_type = llvm::IntegerType::getInt32Ty(context.llvm_context());
  264. llvm::Value* arg_value = context.builder().CreateSExtOrTrunc(
  265. context.GetValue(arg_ids[0]), i32_type);
  266. auto putchar = context.llvm_module().getOrInsertFunction(
  267. "putchar", i32_type, i32_type);
  268. auto* result = context.builder().CreateCall(putchar, {arg_value});
  269. context.SetLocal(inst_id, context.builder().CreateSExtOrTrunc(
  270. result, context.GetTypeOfInst(inst_id)));
  271. return;
  272. }
  273. case SemIR::BuiltinFunctionKind::PrintInt: {
  274. auto* i32_type = llvm::IntegerType::getInt32Ty(context.llvm_context());
  275. auto* ptr_type = llvm::PointerType::get(context.llvm_context(), 0);
  276. auto* printf_type = llvm::FunctionType::get(i32_type, {ptr_type},
  277. /*isVarArg=*/true);
  278. llvm::FunctionCallee printf =
  279. context.llvm_module().getOrInsertFunction("printf", printf_type);
  280. llvm::Value* format_string = context.printf_int_format_string();
  281. llvm::Value* arg_value = context.builder().CreateSExtOrTrunc(
  282. context.GetValue(arg_ids[0]), i32_type);
  283. context.SetLocal(inst_id, context.builder().CreateCall(
  284. printf, {format_string, arg_value}));
  285. return;
  286. }
  287. case SemIR::BuiltinFunctionKind::ReadChar: {
  288. auto* i32_type = llvm::IntegerType::getInt32Ty(context.llvm_context());
  289. auto getchar =
  290. context.llvm_module().getOrInsertFunction("getchar", i32_type);
  291. auto* result = context.builder().CreateCall(getchar, {});
  292. context.SetLocal(inst_id, context.builder().CreateSExtOrTrunc(
  293. result, context.GetTypeOfInst(inst_id)));
  294. return;
  295. }
  296. case SemIR::BuiltinFunctionKind::TypeAnd: {
  297. context.SetLocal(inst_id, context.GetTypeAsValue());
  298. return;
  299. }
  300. case SemIR::BuiltinFunctionKind::BoolMakeType:
  301. case SemIR::BuiltinFunctionKind::CharLiteralMakeType:
  302. case SemIR::BuiltinFunctionKind::FloatLiteralMakeType:
  303. case SemIR::BuiltinFunctionKind::FloatMakeType:
  304. case SemIR::BuiltinFunctionKind::IntLiteralMakeType:
  305. case SemIR::BuiltinFunctionKind::IntMakeTypeSigned:
  306. case SemIR::BuiltinFunctionKind::IntMakeTypeUnsigned:
  307. case SemIR::BuiltinFunctionKind::MaybeUnformedMakeType:
  308. context.SetLocal(inst_id, context.GetTypeAsValue());
  309. return;
  310. case SemIR::BuiltinFunctionKind::IntConvert: {
  311. context.SetLocal(inst_id,
  312. CreateExtOrTrunc(context, context.GetValue(arg_ids[0]),
  313. context.GetTypeOfInst(inst_id),
  314. IsSignedInt(context, arg_ids[0])));
  315. return;
  316. }
  317. case SemIR::BuiltinFunctionKind::IntSNegate: {
  318. // Lower `-x` as `0 - x`.
  319. auto* operand = context.GetValue(arg_ids[0]);
  320. context.SetLocal(
  321. inst_id,
  322. context.builder().CreateSub(
  323. llvm::ConstantInt::getNullValue(operand->getType()), operand, "",
  324. /*HasNUW=*/false,
  325. /*HasNSW=*/SignedOverflowIsUB));
  326. return;
  327. }
  328. case SemIR::BuiltinFunctionKind::IntUNegate: {
  329. // Lower `-x` as `0 - x`.
  330. auto* operand = context.GetValue(arg_ids[0]);
  331. context.SetLocal(
  332. inst_id,
  333. context.builder().CreateSub(
  334. llvm::ConstantInt::getNullValue(operand->getType()), operand));
  335. return;
  336. }
  337. case SemIR::BuiltinFunctionKind::IntComplement: {
  338. // Lower `^x` as `-1 ^ x`.
  339. auto* operand = context.GetValue(arg_ids[0]);
  340. context.SetLocal(
  341. inst_id,
  342. context.builder().CreateXor(
  343. llvm::ConstantInt::getSigned(operand->getType(), -1), operand));
  344. return;
  345. }
  346. case SemIR::BuiltinFunctionKind::IntSAdd:
  347. case SemIR::BuiltinFunctionKind::IntSSub:
  348. case SemIR::BuiltinFunctionKind::IntSMul:
  349. case SemIR::BuiltinFunctionKind::IntSDiv:
  350. case SemIR::BuiltinFunctionKind::IntSMod:
  351. case SemIR::BuiltinFunctionKind::IntUAdd:
  352. case SemIR::BuiltinFunctionKind::IntUSub:
  353. case SemIR::BuiltinFunctionKind::IntUMul:
  354. case SemIR::BuiltinFunctionKind::IntUDiv:
  355. case SemIR::BuiltinFunctionKind::IntUMod:
  356. case SemIR::BuiltinFunctionKind::IntAnd:
  357. case SemIR::BuiltinFunctionKind::IntOr:
  358. case SemIR::BuiltinFunctionKind::IntXor:
  359. case SemIR::BuiltinFunctionKind::IntLeftShift:
  360. case SemIR::BuiltinFunctionKind::IntRightShift:
  361. case SemIR::BuiltinFunctionKind::FloatAdd:
  362. case SemIR::BuiltinFunctionKind::FloatSub:
  363. case SemIR::BuiltinFunctionKind::FloatMul:
  364. case SemIR::BuiltinFunctionKind::FloatDiv: {
  365. context.SetLocal(inst_id, CreateBinaryOperatorForBuiltin(
  366. context, inst_id, builtin_kind,
  367. context.GetValue(arg_ids[0]),
  368. context.GetValue(arg_ids[1])));
  369. return;
  370. }
  371. case SemIR::BuiltinFunctionKind::IntSAddAssign:
  372. case SemIR::BuiltinFunctionKind::IntSSubAssign:
  373. case SemIR::BuiltinFunctionKind::IntSMulAssign:
  374. case SemIR::BuiltinFunctionKind::IntSDivAssign:
  375. case SemIR::BuiltinFunctionKind::IntSModAssign:
  376. case SemIR::BuiltinFunctionKind::IntUAddAssign:
  377. case SemIR::BuiltinFunctionKind::IntUSubAssign:
  378. case SemIR::BuiltinFunctionKind::IntUMulAssign:
  379. case SemIR::BuiltinFunctionKind::IntUDivAssign:
  380. case SemIR::BuiltinFunctionKind::IntUModAssign:
  381. case SemIR::BuiltinFunctionKind::IntAndAssign:
  382. case SemIR::BuiltinFunctionKind::IntOrAssign:
  383. case SemIR::BuiltinFunctionKind::IntXorAssign:
  384. case SemIR::BuiltinFunctionKind::IntLeftShiftAssign:
  385. case SemIR::BuiltinFunctionKind::IntRightShiftAssign:
  386. case SemIR::BuiltinFunctionKind::FloatAddAssign:
  387. case SemIR::BuiltinFunctionKind::FloatSubAssign:
  388. case SemIR::BuiltinFunctionKind::FloatMulAssign:
  389. case SemIR::BuiltinFunctionKind::FloatDivAssign: {
  390. auto* lhs_ptr = context.GetValue(arg_ids[0]);
  391. auto lhs_type = context.GetTypeIdOfInst(arg_ids[0]);
  392. auto pointee_type = lhs_type.GetPointeeType();
  393. auto* lhs_value = context.LoadObject(pointee_type, lhs_ptr);
  394. auto* result = CreateBinaryOperatorForBuiltin(
  395. context, inst_id, builtin_kind, lhs_value,
  396. context.GetValue(arg_ids[1]));
  397. context.StoreObject(pointee_type, result, lhs_ptr);
  398. // TODO: Add a helper to get a "no value representation" value.
  399. context.SetLocal(inst_id,
  400. llvm::PoisonValue::get(context.GetTypeOfInst(inst_id)));
  401. return;
  402. }
  403. case SemIR::BuiltinFunctionKind::IntEq:
  404. case SemIR::BuiltinFunctionKind::IntNeq:
  405. case SemIR::BuiltinFunctionKind::IntLess:
  406. case SemIR::BuiltinFunctionKind::IntLessEq:
  407. case SemIR::BuiltinFunctionKind::IntGreater:
  408. case SemIR::BuiltinFunctionKind::IntGreaterEq:
  409. case SemIR::BuiltinFunctionKind::BoolEq:
  410. case SemIR::BuiltinFunctionKind::BoolNeq: {
  411. HandleIntComparison(context, inst_id, builtin_kind, arg_ids[0],
  412. arg_ids[1]);
  413. return;
  414. }
  415. case SemIR::BuiltinFunctionKind::FloatNegate: {
  416. context.SetLocal(
  417. inst_id, context.builder().CreateFNeg(context.GetValue(arg_ids[0])));
  418. return;
  419. }
  420. case SemIR::BuiltinFunctionKind::FloatEq:
  421. case SemIR::BuiltinFunctionKind::FloatNeq:
  422. case SemIR::BuiltinFunctionKind::FloatLess:
  423. case SemIR::BuiltinFunctionKind::FloatLessEq:
  424. case SemIR::BuiltinFunctionKind::FloatGreater:
  425. case SemIR::BuiltinFunctionKind::FloatGreaterEq: {
  426. context.SetLocal(inst_id, context.builder().CreateFCmp(
  427. GetBuiltinFCmpPredicate(builtin_kind),
  428. context.GetValue(arg_ids[0]),
  429. context.GetValue(arg_ids[1])));
  430. return;
  431. }
  432. case SemIR::BuiltinFunctionKind::CharConvertChecked:
  433. case SemIR::BuiltinFunctionKind::FloatConvertChecked:
  434. case SemIR::BuiltinFunctionKind::IntConvertChecked:
  435. case SemIR::BuiltinFunctionKind::TypeCanAggregateDestroy: {
  436. // TODO: Check this statically.
  437. CARBON_CHECK(builtin_kind.IsCompTimeOnly(
  438. context.sem_ir(), arg_ids,
  439. context.sem_ir().insts().Get(inst_id).type_id()));
  440. CARBON_FATAL("Missing constant value for call to comptime-only function");
  441. }
  442. case SemIR::BuiltinFunctionKind::TypeAggregateDestroy:
  443. // TODO: Destroy aggregate members.
  444. return;
  445. }
  446. CARBON_FATAL("Unsupported builtin call.");
  447. }
  448. static auto HandleVirtualCall(FunctionContext& context,
  449. llvm::ArrayRef<llvm::Value*> args,
  450. const SemIR::File* callee_file,
  451. const SemIR::Function& function,
  452. const SemIR::CalleeFunction& callee_function)
  453. -> llvm::CallInst* {
  454. CARBON_CHECK(!args.empty(),
  455. "Virtual functions must have at least one parameter");
  456. auto* ptr_type =
  457. llvm::PointerType::get(context.llvm_context(), /*AddressSpace=*/0);
  458. // The vtable pointer is always at the start of the object in the Carbon
  459. // ABI, so a pointer to the object is a pointer to the vtable pointer - load
  460. // that to get a pointer to the vtable.
  461. // TODO: Handle the case in C++ interop where the vtable pointer isn't at
  462. // the start of the object.
  463. // TODO: Use `context.LoadObject`.
  464. auto* vtable = context.builder().CreateLoad(ptr_type, args.front(), "vtable");
  465. auto* i32_type = llvm::IntegerType::getInt32Ty(context.llvm_context());
  466. auto* pointer_type =
  467. llvm::PointerType::get(context.llvm_context(), /* address space */ 0);
  468. auto function_type_info =
  469. context.GetFileContext(callee_file)
  470. .BuildFunctionTypeInfo(function,
  471. callee_function.resolved_specific_id);
  472. llvm::Value* virtual_fn;
  473. if (function.clang_decl_id.has_value()) {
  474. // Use absolute vtables for clang interop - the itanium vtable contains
  475. // function pointers.
  476. auto* virtual_function_pointer_address = context.builder().CreateGEP(
  477. pointer_type, vtable,
  478. {llvm::ConstantInt::get(
  479. i32_type, static_cast<uint64_t>(function.virtual_index))});
  480. virtual_fn = context.builder().CreateLoad(
  481. pointer_type, virtual_function_pointer_address, "memptr.virtualfn");
  482. } else {
  483. // For Carbon, use Relative VTables as pioneered by Fuchsia:
  484. // https://llvm.org/devmtg/2021-11/slides/2021-RelativeVTablesinC.pdf
  485. // In this case, the vtable contains an offset from the vtable itself to the
  486. // function in question. This avoids the use of link-time relocations in the
  487. // vtable (making object files smaller, improving link time) - at the cost
  488. // of extra instructions to resolve the offset at the call-site.
  489. // This uses the `llvm.load.relative` intrinsic (
  490. // https://llvm.org/docs/LangRef.html#llvm-load-relative-intrinsic ) that
  491. // essentially does the arithmetic in one-shot: ptr + *(ptr + offset)
  492. virtual_fn = context.builder().CreateCall(
  493. llvm::Intrinsic::getOrInsertDeclaration(
  494. &context.llvm_module(), llvm::Intrinsic::load_relative, {i32_type}),
  495. {vtable,
  496. llvm::ConstantInt::get(
  497. i32_type, static_cast<uint64_t>(function.virtual_index) * 4)});
  498. }
  499. return context.builder().CreateCall(function_type_info.type, virtual_fn,
  500. args);
  501. }
  502. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  503. SemIR::Call inst) -> void {
  504. llvm::ArrayRef<SemIR::InstId> arg_ids =
  505. context.sem_ir().inst_blocks().Get(inst.args_id);
  506. // TODO: This duplicates the SpecificId handling in `GetCallee`.
  507. // TODO: Should the `bound_method` be removed when forming the `call`
  508. // instruction? The `self` parameter is transferred into the call argument
  509. // list.
  510. auto callee_id = inst.callee_id;
  511. if (auto bound_method =
  512. context.sem_ir().insts().TryGetAs<SemIR::BoundMethod>(callee_id)) {
  513. callee_id = bound_method->function_decl_id;
  514. }
  515. // Map to the callee in the specific. This might be in a different file than
  516. // the one we're currently lowering.
  517. const auto* callee_file = &context.sem_ir();
  518. if (context.specific_id().has_value()) {
  519. auto [const_file, const_id] = GetConstantValueInSpecific(
  520. context.specific_sem_ir(), context.specific_id(), context.sem_ir(),
  521. callee_id);
  522. callee_file = const_file;
  523. callee_id = const_file->constant_values().GetInstIdIfValid(const_id);
  524. CARBON_CHECK(callee_id.has_value());
  525. }
  526. auto callee_function = SemIR::GetCalleeAsFunction(*callee_file, callee_id);
  527. const SemIR::Function& function =
  528. callee_file->functions().Get(callee_function.function_id);
  529. context.AddCallToCurrentFingerprint(callee_file->check_ir_id(),
  530. callee_function.function_id,
  531. callee_function.resolved_specific_id);
  532. if (auto builtin_kind = function.builtin_function_kind();
  533. builtin_kind != SemIR::BuiltinFunctionKind::None) {
  534. HandleBuiltinCall(context, inst_id, builtin_kind, arg_ids);
  535. return;
  536. }
  537. std::vector<llvm::Value*> args;
  538. auto inst_type = context.GetTypeIdOfInst(inst_id);
  539. bool call_has_return_slot =
  540. SemIR::ReturnTypeInfo::ForType(context.sem_ir(), inst.type_id)
  541. .has_return_slot();
  542. if (context.GetReturnTypeInfo(inst_type).info.has_return_slot()) {
  543. CARBON_CHECK(call_has_return_slot);
  544. args.push_back(context.GetValue(arg_ids.consume_back()));
  545. } else if (call_has_return_slot) {
  546. // Call instruction has a return slot but this specific callee does not.
  547. // Just ignore it.
  548. arg_ids.consume_back();
  549. }
  550. for (auto arg_id : arg_ids) {
  551. auto arg_type = context.GetTypeIdOfInst(arg_id);
  552. if (context.GetValueRepr(arg_type).repr.kind != SemIR::ValueRepr::None) {
  553. args.push_back(context.GetValue(arg_id));
  554. }
  555. }
  556. llvm::CallInst* call;
  557. if (function.virtual_modifier == SemIR::Function::VirtualModifier::None) {
  558. auto* callee =
  559. context.GetFileContext(callee_file)
  560. .GetOrCreateFunction(callee_function.function_id,
  561. callee_function.resolved_specific_id);
  562. auto describe_call = [&] {
  563. RawStringOstream out;
  564. out << "call ";
  565. callee->printAsOperand(out);
  566. out << "(";
  567. llvm::ListSeparator sep;
  568. for (auto* arg : args) {
  569. out << sep;
  570. arg->printAsOperand(out);
  571. }
  572. out << ")\n";
  573. callee->print(out);
  574. return out.TakeStr();
  575. };
  576. CARBON_CHECK(callee->arg_size() == args.size(),
  577. "Argument count mismatch: {0}", describe_call());
  578. call = context.builder().CreateCall(callee, args);
  579. } else {
  580. call = HandleVirtualCall(context, args, callee_file, function,
  581. callee_function);
  582. }
  583. context.SetLocal(inst_id, call);
  584. }
  585. } // namespace Carbon::Lower