handle_call.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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. return CreateIntShift(context,
  217. lhs_type_file->types().IsSignedInt(lhs_type_id)
  218. ? llvm::Instruction::AShr
  219. : llvm::Instruction::LShr,
  220. lhs, rhs);
  221. }
  222. case SemIR::BuiltinFunctionKind::FloatAdd:
  223. case SemIR::BuiltinFunctionKind::FloatAddAssign: {
  224. return context.builder().CreateFAdd(lhs, rhs);
  225. }
  226. case SemIR::BuiltinFunctionKind::FloatSub:
  227. case SemIR::BuiltinFunctionKind::FloatSubAssign: {
  228. return context.builder().CreateFSub(lhs, rhs);
  229. }
  230. case SemIR::BuiltinFunctionKind::FloatMul:
  231. case SemIR::BuiltinFunctionKind::FloatMulAssign: {
  232. return context.builder().CreateFMul(lhs, rhs);
  233. }
  234. case SemIR::BuiltinFunctionKind::FloatDiv:
  235. case SemIR::BuiltinFunctionKind::FloatDivAssign: {
  236. return context.builder().CreateFDiv(lhs, rhs);
  237. }
  238. default: {
  239. CARBON_FATAL("Unexpected binary operator {0}", builtin_kind);
  240. }
  241. }
  242. }
  243. // Handles a call to a builtin function.
  244. static auto HandleBuiltinCall(FunctionContext& context, SemIR::InstId inst_id,
  245. SemIR::BuiltinFunctionKind builtin_kind,
  246. llvm::ArrayRef<SemIR::InstId> arg_ids) -> void {
  247. // TODO: Consider setting this to true in the performance build mode if the
  248. // result type is a signed integer type.
  249. constexpr bool SignedOverflowIsUB = false;
  250. // TODO: Move the instruction names here into InstNamer.
  251. switch (builtin_kind) {
  252. case SemIR::BuiltinFunctionKind::None:
  253. CARBON_FATAL("No callee in function call.");
  254. case SemIR::BuiltinFunctionKind::NoOp:
  255. return;
  256. case SemIR::BuiltinFunctionKind::PrimitiveCopy:
  257. context.SetLocal(inst_id, context.GetValue(arg_ids[0]));
  258. return;
  259. case SemIR::BuiltinFunctionKind::PrintChar: {
  260. auto* i32_type = llvm::IntegerType::getInt32Ty(context.llvm_context());
  261. llvm::Value* arg_value = context.builder().CreateSExtOrTrunc(
  262. context.GetValue(arg_ids[0]), i32_type);
  263. auto putchar = context.llvm_module().getOrInsertFunction(
  264. "putchar", i32_type, i32_type);
  265. auto* result = context.builder().CreateCall(putchar, {arg_value});
  266. context.SetLocal(inst_id, context.builder().CreateSExtOrTrunc(
  267. result, context.GetTypeOfInst(inst_id)));
  268. return;
  269. }
  270. case SemIR::BuiltinFunctionKind::PrintInt: {
  271. auto* i32_type = llvm::IntegerType::getInt32Ty(context.llvm_context());
  272. auto* ptr_type = llvm::PointerType::get(context.llvm_context(), 0);
  273. auto* printf_type = llvm::FunctionType::get(i32_type, {ptr_type},
  274. /*isVarArg=*/true);
  275. llvm::FunctionCallee printf =
  276. context.llvm_module().getOrInsertFunction("printf", printf_type);
  277. llvm::Value* format_string = context.printf_int_format_string();
  278. llvm::Value* arg_value = context.builder().CreateSExtOrTrunc(
  279. context.GetValue(arg_ids[0]), i32_type);
  280. context.SetLocal(inst_id, context.builder().CreateCall(
  281. printf, {format_string, arg_value}));
  282. return;
  283. }
  284. case SemIR::BuiltinFunctionKind::ReadChar: {
  285. auto* i32_type = llvm::IntegerType::getInt32Ty(context.llvm_context());
  286. auto getchar =
  287. context.llvm_module().getOrInsertFunction("getchar", i32_type);
  288. auto* result = context.builder().CreateCall(getchar, {});
  289. context.SetLocal(inst_id, context.builder().CreateSExtOrTrunc(
  290. result, context.GetTypeOfInst(inst_id)));
  291. return;
  292. }
  293. case SemIR::BuiltinFunctionKind::TypeAnd: {
  294. context.SetLocal(inst_id, context.GetTypeAsValue());
  295. return;
  296. }
  297. case SemIR::BuiltinFunctionKind::BoolMakeType:
  298. case SemIR::BuiltinFunctionKind::CharLiteralMakeType:
  299. case SemIR::BuiltinFunctionKind::FloatLiteralMakeType:
  300. case SemIR::BuiltinFunctionKind::FloatMakeType:
  301. case SemIR::BuiltinFunctionKind::IntLiteralMakeType:
  302. case SemIR::BuiltinFunctionKind::IntMakeTypeSigned:
  303. case SemIR::BuiltinFunctionKind::IntMakeTypeUnsigned:
  304. case SemIR::BuiltinFunctionKind::MaybeUnformedMakeType:
  305. context.SetLocal(inst_id, context.GetTypeAsValue());
  306. return;
  307. case SemIR::BuiltinFunctionKind::IntConvert: {
  308. context.SetLocal(inst_id,
  309. CreateExtOrTrunc(context, context.GetValue(arg_ids[0]),
  310. context.GetTypeOfInst(inst_id),
  311. IsSignedInt(context, arg_ids[0])));
  312. return;
  313. }
  314. case SemIR::BuiltinFunctionKind::IntSNegate: {
  315. // Lower `-x` as `0 - x`.
  316. auto* operand = context.GetValue(arg_ids[0]);
  317. context.SetLocal(
  318. inst_id,
  319. context.builder().CreateSub(
  320. llvm::ConstantInt::getNullValue(operand->getType()), operand, "",
  321. /*HasNUW=*/false,
  322. /*HasNSW=*/SignedOverflowIsUB));
  323. return;
  324. }
  325. case SemIR::BuiltinFunctionKind::IntUNegate: {
  326. // Lower `-x` as `0 - x`.
  327. auto* operand = context.GetValue(arg_ids[0]);
  328. context.SetLocal(
  329. inst_id,
  330. context.builder().CreateSub(
  331. llvm::ConstantInt::getNullValue(operand->getType()), operand));
  332. return;
  333. }
  334. case SemIR::BuiltinFunctionKind::IntComplement: {
  335. // Lower `^x` as `-1 ^ x`.
  336. auto* operand = context.GetValue(arg_ids[0]);
  337. context.SetLocal(
  338. inst_id,
  339. context.builder().CreateXor(
  340. llvm::ConstantInt::getSigned(operand->getType(), -1), operand));
  341. return;
  342. }
  343. case SemIR::BuiltinFunctionKind::IntSAdd:
  344. case SemIR::BuiltinFunctionKind::IntSSub:
  345. case SemIR::BuiltinFunctionKind::IntSMul:
  346. case SemIR::BuiltinFunctionKind::IntSDiv:
  347. case SemIR::BuiltinFunctionKind::IntSMod:
  348. case SemIR::BuiltinFunctionKind::IntUAdd:
  349. case SemIR::BuiltinFunctionKind::IntUSub:
  350. case SemIR::BuiltinFunctionKind::IntUMul:
  351. case SemIR::BuiltinFunctionKind::IntUDiv:
  352. case SemIR::BuiltinFunctionKind::IntUMod:
  353. case SemIR::BuiltinFunctionKind::IntAnd:
  354. case SemIR::BuiltinFunctionKind::IntOr:
  355. case SemIR::BuiltinFunctionKind::IntXor:
  356. case SemIR::BuiltinFunctionKind::IntLeftShift:
  357. case SemIR::BuiltinFunctionKind::IntRightShift:
  358. case SemIR::BuiltinFunctionKind::FloatAdd:
  359. case SemIR::BuiltinFunctionKind::FloatSub:
  360. case SemIR::BuiltinFunctionKind::FloatMul:
  361. case SemIR::BuiltinFunctionKind::FloatDiv: {
  362. context.SetLocal(inst_id, CreateBinaryOperatorForBuiltin(
  363. context, inst_id, builtin_kind,
  364. context.GetValue(arg_ids[0]),
  365. context.GetValue(arg_ids[1])));
  366. return;
  367. }
  368. case SemIR::BuiltinFunctionKind::IntSAddAssign:
  369. case SemIR::BuiltinFunctionKind::IntSSubAssign:
  370. case SemIR::BuiltinFunctionKind::IntSMulAssign:
  371. case SemIR::BuiltinFunctionKind::IntSDivAssign:
  372. case SemIR::BuiltinFunctionKind::IntSModAssign:
  373. case SemIR::BuiltinFunctionKind::IntUAddAssign:
  374. case SemIR::BuiltinFunctionKind::IntUSubAssign:
  375. case SemIR::BuiltinFunctionKind::IntUMulAssign:
  376. case SemIR::BuiltinFunctionKind::IntUDivAssign:
  377. case SemIR::BuiltinFunctionKind::IntUModAssign:
  378. case SemIR::BuiltinFunctionKind::IntAndAssign:
  379. case SemIR::BuiltinFunctionKind::IntOrAssign:
  380. case SemIR::BuiltinFunctionKind::IntXorAssign:
  381. case SemIR::BuiltinFunctionKind::IntLeftShiftAssign:
  382. case SemIR::BuiltinFunctionKind::IntRightShiftAssign:
  383. case SemIR::BuiltinFunctionKind::FloatAddAssign:
  384. case SemIR::BuiltinFunctionKind::FloatSubAssign:
  385. case SemIR::BuiltinFunctionKind::FloatMulAssign:
  386. case SemIR::BuiltinFunctionKind::FloatDivAssign: {
  387. auto* lhs_ptr = context.GetValue(arg_ids[0]);
  388. auto lhs_type = context.GetTypeIdOfInst(arg_ids[0]);
  389. auto* lhs_value = context.LoadObject(lhs_type, lhs_ptr);
  390. auto* result = CreateBinaryOperatorForBuiltin(
  391. context, inst_id, builtin_kind, lhs_value,
  392. context.GetValue(arg_ids[1]));
  393. context.StoreObject(lhs_type, result, lhs_ptr);
  394. // TODO: Add a helper to get a "no value representation" value.
  395. context.SetLocal(inst_id,
  396. llvm::PoisonValue::get(context.GetTypeOfInst(inst_id)));
  397. return;
  398. }
  399. case SemIR::BuiltinFunctionKind::IntEq:
  400. case SemIR::BuiltinFunctionKind::IntNeq:
  401. case SemIR::BuiltinFunctionKind::IntLess:
  402. case SemIR::BuiltinFunctionKind::IntLessEq:
  403. case SemIR::BuiltinFunctionKind::IntGreater:
  404. case SemIR::BuiltinFunctionKind::IntGreaterEq:
  405. case SemIR::BuiltinFunctionKind::BoolEq:
  406. case SemIR::BuiltinFunctionKind::BoolNeq: {
  407. HandleIntComparison(context, inst_id, builtin_kind, arg_ids[0],
  408. arg_ids[1]);
  409. return;
  410. }
  411. case SemIR::BuiltinFunctionKind::FloatNegate: {
  412. context.SetLocal(
  413. inst_id, context.builder().CreateFNeg(context.GetValue(arg_ids[0])));
  414. return;
  415. }
  416. case SemIR::BuiltinFunctionKind::FloatEq:
  417. case SemIR::BuiltinFunctionKind::FloatNeq:
  418. case SemIR::BuiltinFunctionKind::FloatLess:
  419. case SemIR::BuiltinFunctionKind::FloatLessEq:
  420. case SemIR::BuiltinFunctionKind::FloatGreater:
  421. case SemIR::BuiltinFunctionKind::FloatGreaterEq: {
  422. context.SetLocal(inst_id, context.builder().CreateFCmp(
  423. GetBuiltinFCmpPredicate(builtin_kind),
  424. context.GetValue(arg_ids[0]),
  425. context.GetValue(arg_ids[1])));
  426. return;
  427. }
  428. case SemIR::BuiltinFunctionKind::CharConvertChecked:
  429. case SemIR::BuiltinFunctionKind::FloatConvertChecked:
  430. case SemIR::BuiltinFunctionKind::IntConvertChecked:
  431. case SemIR::BuiltinFunctionKind::TypeCanDestroy: {
  432. // TODO: Check this statically.
  433. CARBON_CHECK(builtin_kind.IsCompTimeOnly(
  434. context.sem_ir(), arg_ids,
  435. context.sem_ir().insts().Get(inst_id).type_id()));
  436. CARBON_FATAL("Missing constant value for call to comptime-only function");
  437. }
  438. case SemIR::BuiltinFunctionKind::PointerMakeNull: {
  439. context.SetLocal(inst_id,
  440. llvm::ConstantPointerNull::get(
  441. llvm::PointerType::get(context.llvm_context(),
  442. /*AddressSpace=*/0)));
  443. return;
  444. }
  445. case SemIR::BuiltinFunctionKind::PointerIsNull: {
  446. context.SetLocal(inst_id, context.builder().CreateIsNull(
  447. context.GetValue(arg_ids[0])));
  448. return;
  449. }
  450. case SemIR::BuiltinFunctionKind::TypeDestroy:
  451. // TODO: Destroy aggregate members.
  452. return;
  453. }
  454. CARBON_FATAL("Unsupported builtin call.");
  455. }
  456. static auto HandleVirtualCall(FunctionContext& context,
  457. llvm::ArrayRef<llvm::Value*> args,
  458. const SemIR::File* callee_file,
  459. const SemIR::Function& function,
  460. const SemIR::CalleeFunction& callee_function)
  461. -> llvm::CallInst* {
  462. CARBON_CHECK(!args.empty(),
  463. "Virtual functions must have at least one parameter");
  464. auto* ptr_type =
  465. llvm::PointerType::get(context.llvm_context(), /*AddressSpace=*/0);
  466. // The vtable pointer is always at the start of the object in the Carbon
  467. // ABI, so a pointer to the object is a pointer to the vtable pointer - load
  468. // that to get a pointer to the vtable.
  469. // TODO: Handle the case in C++ interop where the vtable pointer isn't at
  470. // the start of the object.
  471. // TODO: Use `context.LoadObject`.
  472. auto* vtable = context.builder().CreateLoad(ptr_type, args.front(), "vtable");
  473. auto* i32_type = llvm::IntegerType::getInt32Ty(context.llvm_context());
  474. auto* pointer_type =
  475. llvm::PointerType::get(context.llvm_context(), /* address space */ 0);
  476. auto function_type_info =
  477. context.GetFileContext(callee_file)
  478. .BuildFunctionTypeInfo(function,
  479. callee_function.resolved_specific_id);
  480. llvm::Value* virtual_fn;
  481. if (function.clang_decl_id.has_value()) {
  482. // Use absolute vtables for clang interop - the itanium vtable contains
  483. // function pointers.
  484. auto* virtual_function_pointer_address = context.builder().CreateGEP(
  485. pointer_type, vtable,
  486. {llvm::ConstantInt::get(
  487. i32_type, static_cast<uint64_t>(function.virtual_index))});
  488. virtual_fn = context.builder().CreateLoad(
  489. pointer_type, virtual_function_pointer_address, "memptr.virtualfn");
  490. } else {
  491. // For Carbon, use Relative VTables as pioneered by Fuchsia:
  492. // https://llvm.org/devmtg/2021-11/slides/2021-RelativeVTablesinC.pdf
  493. // In this case, the vtable contains an offset from the vtable itself to the
  494. // function in question. This avoids the use of link-time relocations in the
  495. // vtable (making object files smaller, improving link time) - at the cost
  496. // of extra instructions to resolve the offset at the call-site.
  497. // This uses the `llvm.load.relative` intrinsic (
  498. // https://llvm.org/docs/LangRef.html#llvm-load-relative-intrinsic ) that
  499. // essentially does the arithmetic in one-shot: ptr + *(ptr + offset)
  500. virtual_fn = context.builder().CreateCall(
  501. llvm::Intrinsic::getOrInsertDeclaration(
  502. &context.llvm_module(), llvm::Intrinsic::load_relative, {i32_type}),
  503. {vtable,
  504. llvm::ConstantInt::get(
  505. i32_type, static_cast<uint64_t>(function.virtual_index) * 4)});
  506. }
  507. return context.builder().CreateCall(function_type_info.type, virtual_fn,
  508. args);
  509. }
  510. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  511. SemIR::Call inst) -> void {
  512. llvm::ArrayRef<SemIR::InstId> arg_ids =
  513. context.sem_ir().inst_blocks().Get(inst.args_id);
  514. // TODO: This duplicates the SpecificId handling in `GetCallee`.
  515. // TODO: Should the `bound_method` be removed when forming the `call`
  516. // instruction? The `self` parameter is transferred into the call argument
  517. // list.
  518. auto callee_id = inst.callee_id;
  519. if (auto bound_method =
  520. context.sem_ir().insts().TryGetAs<SemIR::BoundMethod>(callee_id)) {
  521. callee_id = bound_method->function_decl_id;
  522. }
  523. // Map to the callee in the specific. This might be in a different file than
  524. // the one we're currently lowering.
  525. const auto* callee_file = &context.sem_ir();
  526. if (context.specific_id().has_value()) {
  527. auto [const_file, const_id] = GetConstantValueInSpecific(
  528. context.specific_sem_ir(), context.specific_id(), context.sem_ir(),
  529. callee_id);
  530. callee_file = const_file;
  531. callee_id = const_file->constant_values().GetInstIdIfValid(const_id);
  532. CARBON_CHECK(callee_id.has_value());
  533. }
  534. auto callee_function = SemIR::GetCalleeAsFunction(*callee_file, callee_id);
  535. const SemIR::Function& function =
  536. callee_file->functions().Get(callee_function.function_id);
  537. context.AddCallToCurrentFingerprint(callee_file->check_ir_id(),
  538. callee_function.function_id,
  539. callee_function.resolved_specific_id);
  540. if (auto builtin_kind = function.builtin_function_kind();
  541. builtin_kind != SemIR::BuiltinFunctionKind::None) {
  542. HandleBuiltinCall(context, inst_id, builtin_kind, arg_ids);
  543. return;
  544. }
  545. std::vector<llvm::Value*> args;
  546. auto inst_type = context.GetTypeIdOfInst(inst_id);
  547. bool call_has_return_slot =
  548. SemIR::ReturnTypeInfo::ForType(context.sem_ir(), inst.type_id)
  549. .has_return_slot();
  550. if (context.GetReturnTypeInfo(inst_type).info.has_return_slot()) {
  551. CARBON_CHECK(call_has_return_slot);
  552. args.push_back(context.GetValue(arg_ids.consume_back()));
  553. } else if (call_has_return_slot) {
  554. // Call instruction has a return slot but this specific callee does not.
  555. // Just ignore it.
  556. arg_ids.consume_back();
  557. }
  558. for (auto arg_id : arg_ids) {
  559. auto arg_type = context.GetTypeIdOfInst(arg_id);
  560. if (context.GetValueRepr(arg_type).repr.kind != SemIR::ValueRepr::None) {
  561. args.push_back(context.GetValue(arg_id));
  562. }
  563. }
  564. llvm::CallInst* call;
  565. if (function.virtual_modifier == SemIR::Function::VirtualModifier::None) {
  566. auto* callee =
  567. context.GetFileContext(callee_file)
  568. .GetOrCreateFunction(callee_function.function_id,
  569. callee_function.resolved_specific_id);
  570. auto describe_call = [&] {
  571. RawStringOstream out;
  572. out << "call ";
  573. callee->printAsOperand(out);
  574. out << "(";
  575. llvm::ListSeparator sep;
  576. for (auto* arg : args) {
  577. out << sep;
  578. arg->printAsOperand(out);
  579. }
  580. out << ")\n";
  581. callee->print(out);
  582. return out.TakeStr();
  583. };
  584. CARBON_CHECK(callee->arg_size() == args.size(),
  585. "Argument count mismatch: {0}", describe_call());
  586. call = context.builder().CreateCall(callee, args);
  587. } else {
  588. call = HandleVirtualCall(context, args, callee_file, function,
  589. callee_function);
  590. }
  591. context.SetLocal(inst_id, call);
  592. }
  593. } // namespace Carbon::Lower