builtin_function_kind.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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 "toolchain/sem_ir/builtin_function_kind.h"
  5. #include <utility>
  6. #include "toolchain/sem_ir/file.h"
  7. #include "toolchain/sem_ir/ids.h"
  8. #include "toolchain/sem_ir/typed_insts.h"
  9. namespace Carbon::SemIR {
  10. // A function that validates that a builtin was declared properly.
  11. using ValidateFn = auto(const File& sem_ir, llvm::ArrayRef<TypeId> arg_types,
  12. TypeId return_type) -> bool;
  13. namespace {
  14. // Information about a builtin function.
  15. struct BuiltinInfo {
  16. llvm::StringLiteral name;
  17. ValidateFn* validate;
  18. };
  19. // The maximum number of type parameters any builtin needs.
  20. constexpr int MaxTypeParams = 2;
  21. // State used when validating a builtin signature that persists between
  22. // individual checks.
  23. struct ValidateState {
  24. // The type values of type parameters in the builtin signature. Invalid if
  25. // either no value has been deduced yet or the parameter is not used.
  26. TypeId type_params[MaxTypeParams] = {TypeId::None, TypeId::None};
  27. };
  28. template <typename TypeConstraint>
  29. auto Check(const File& sem_ir, ValidateState& state, TypeId type_id) -> bool;
  30. // Constraint that a type is generic type parameter `I` of the builtin,
  31. // satisfying `TypeConstraint`. See ValidateSignature for details.
  32. template <int I, typename TypeConstraint>
  33. struct TypeParam {
  34. static_assert(I >= 0 && I < MaxTypeParams);
  35. static auto Check(const File& sem_ir, ValidateState& state, TypeId type_id)
  36. -> bool {
  37. if (state.type_params[I].has_value() && type_id != state.type_params[I]) {
  38. return false;
  39. }
  40. if (!TypeConstraint::Check(sem_ir, state, type_id)) {
  41. return false;
  42. }
  43. state.type_params[I] = type_id;
  44. return true;
  45. }
  46. };
  47. // Constraint that a type is a specific builtin. See ValidateSignature for
  48. // details.
  49. template <const TypeInstId& BuiltinId>
  50. struct BuiltinType {
  51. static auto Check(const File& sem_ir, ValidateState& /*state*/,
  52. TypeId type_id) -> bool {
  53. return sem_ir.types().GetInstId(type_id) == BuiltinId;
  54. }
  55. };
  56. // Constraint that a type is a pointer to another type. See ValidateSignature
  57. // for details.
  58. template <typename PointeeT>
  59. struct PointerTo {
  60. static auto Check(const File& sem_ir, ValidateState& state, TypeId type_id)
  61. -> bool {
  62. if (!sem_ir.types().Is<PointerType>(type_id)) {
  63. return false;
  64. }
  65. return Check<PointeeT>(sem_ir, state, sem_ir.GetPointeeType(type_id));
  66. }
  67. };
  68. // Constraint that a type is `()`, used as the return type of builtin functions
  69. // with no return value.
  70. struct NoReturn {
  71. static auto Check(const File& sem_ir, ValidateState& /*state*/,
  72. TypeId type_id) -> bool {
  73. auto tuple = sem_ir.types().TryGetAs<TupleType>(type_id);
  74. if (!tuple) {
  75. return false;
  76. }
  77. return sem_ir.inst_blocks().Get(tuple->type_elements_id).empty();
  78. }
  79. };
  80. // Constraint that a type is `bool`.
  81. using Bool = BuiltinType<BoolType::TypeInstId>;
  82. // Constraint that a type is `Core.CharLiteral`.
  83. using CharLiteral = BuiltinType<CharLiteralType::TypeInstId>;
  84. // Constraint that a type is `u8` or an adapted type, including `Core.Char`.
  85. struct CharCompatible {
  86. static auto Check(const File& sem_ir, ValidateState& /*state*/,
  87. TypeId type_id) -> bool {
  88. auto int_info = sem_ir.types().TryGetIntTypeInfo(type_id);
  89. if (!int_info) {
  90. // Not an integer.
  91. return false;
  92. }
  93. if (!int_info->bit_width.has_value() || int_info->is_signed) {
  94. // Must be unsigned.
  95. return false;
  96. }
  97. return sem_ir.ints().Get(int_info->bit_width) == 8;
  98. }
  99. };
  100. // Constraint that requires the type to be a sized integer type.
  101. struct AnySizedInt {
  102. static auto Check(const File& sem_ir, ValidateState& /*state*/,
  103. TypeId type_id) -> bool {
  104. return sem_ir.types().Is<IntType>(type_id);
  105. }
  106. };
  107. // Constraint that requires the type to be an integer type: either a sized
  108. // integer type or a literal.
  109. struct AnyInt {
  110. static auto Check(const File& sem_ir, ValidateState& state, TypeId type_id)
  111. -> bool {
  112. return AnySizedInt::Check(sem_ir, state, type_id) ||
  113. BuiltinType<IntLiteralType::TypeInstId>::Check(sem_ir, state,
  114. type_id);
  115. }
  116. };
  117. // Constraint that requires the type to be a sized floating-point type.
  118. struct AnySizedFloat {
  119. static auto Check(const File& sem_ir, ValidateState& /*state*/,
  120. TypeId type_id) -> bool {
  121. return sem_ir.types().Is<FloatType>(type_id);
  122. }
  123. };
  124. // Constraint that requires the type to be a float type: either a sized float
  125. // type or a literal.
  126. struct AnyFloat {
  127. static auto Check(const File& sem_ir, ValidateState& state, TypeId type_id)
  128. -> bool {
  129. return AnySizedFloat::Check(sem_ir, state, type_id) ||
  130. BuiltinType<LegacyFloatType::TypeInstId>::Check(sem_ir, state,
  131. type_id);
  132. }
  133. };
  134. // Constraint that requires the type to be the type type.
  135. using Type = BuiltinType<TypeType::TypeInstId>;
  136. // Checks that the specified type matches the given type constraint.
  137. template <typename TypeConstraint>
  138. auto Check(const File& sem_ir, ValidateState& state, TypeId type_id) -> bool {
  139. while (type_id.has_value()) {
  140. // Allow a type that satisfies the constraint.
  141. if (TypeConstraint::Check(sem_ir, state, type_id)) {
  142. return true;
  143. }
  144. // Also allow a class type that adapts a matching type.
  145. auto class_type = sem_ir.types().TryGetAs<ClassType>(type_id);
  146. if (!class_type) {
  147. break;
  148. }
  149. type_id = sem_ir.classes()
  150. .Get(class_type->class_id)
  151. .GetAdaptedType(sem_ir, class_type->specific_id);
  152. }
  153. return false;
  154. }
  155. } // namespace
  156. // Validates that this builtin has a signature matching the specified signature.
  157. //
  158. // `SignatureFnType` is a C++ function type that describes the signature that is
  159. // expected for this builtin. For example, `auto (AnyInt, AnyInt) -> AnyInt`
  160. // specifies that the builtin takes values of two integer types and returns a
  161. // value of a third integer type. Types used within the signature should provide
  162. // a `Check` function that validates that the Carbon type is expected:
  163. //
  164. // auto Check(const File&, ValidateState&, TypeId) -> bool;
  165. //
  166. // To constrain that the same type is used in multiple places in the signature,
  167. // `TypeParam<I, T>` can be used. For example:
  168. //
  169. // auto (TypeParam<0, AnyInt>, AnyInt) -> TypeParam<0, AnyInt>
  170. //
  171. // describes a builtin that takes two integers, and whose return type matches
  172. // its first parameter type. For convenience, typedefs for `TypeParam<I, T>`
  173. // are used in the descriptions of the builtins.
  174. template <typename SignatureFnType>
  175. static auto ValidateSignature(const File& sem_ir,
  176. llvm::ArrayRef<TypeId> arg_types,
  177. TypeId return_type) -> bool {
  178. using SignatureTraits = llvm::function_traits<SignatureFnType*>;
  179. ValidateState state;
  180. // Must have expected number of arguments.
  181. if (arg_types.size() != SignatureTraits::num_args) {
  182. return false;
  183. }
  184. // Argument types must match.
  185. if (![&]<size_t... Indexes>(std::index_sequence<Indexes...>) {
  186. return ((Check<typename SignatureTraits::template arg_t<Indexes>>(
  187. sem_ir, state, arg_types[Indexes])) &&
  188. ...);
  189. }(std::make_index_sequence<SignatureTraits::num_args>())) {
  190. return false;
  191. }
  192. // Result type must match.
  193. if (!Check<typename SignatureTraits::result_t>(sem_ir, state, return_type)) {
  194. return false;
  195. }
  196. return true;
  197. }
  198. // Validates the signature for NoOp. This ignores all arguments, only validating
  199. // that the return type is compatible.
  200. static auto ValidateNoOpSignature(const File& sem_ir,
  201. llvm::ArrayRef<TypeId> /*arg_types*/,
  202. TypeId return_type) -> bool {
  203. ValidateState state;
  204. return Check<NoReturn>(sem_ir, state, return_type);
  205. }
  206. // Descriptions of builtin functions follow. For each builtin, a corresponding
  207. // `BuiltinInfo` constant is declared describing properties of that builtin.
  208. namespace BuiltinFunctionInfo {
  209. // Convenience name used in the builtin type signatures below for a first
  210. // generic type parameter that is constrained to be an integer type.
  211. using IntT = TypeParam<0, AnyInt>;
  212. // Convenience name used in the builtin type signatures below for a second
  213. // generic type parameter that is constrained to be an integer type.
  214. using IntU = TypeParam<1, AnyInt>;
  215. // Convenience name used in the builtin type signatures below for a first
  216. // generic type parameter that is constrained to be a sized integer type.
  217. using SizedIntT = TypeParam<0, AnySizedInt>;
  218. // Convenience name used in the builtin type signatures below for a second
  219. // generic type parameter that is constrained to be a sized integer type.
  220. using SizedIntU = TypeParam<1, AnySizedInt>;
  221. // Convenience name used in the builtin type signatures below for a first
  222. // generic type parameter that is constrained to be an float type.
  223. using FloatT = TypeParam<0, AnyFloat>;
  224. // Convenience name used in the builtin type signatures below for a second
  225. // generic type parameter that is constrained to be an float type.
  226. using FloatU = TypeParam<1, AnyFloat>;
  227. // Not a builtin function.
  228. constexpr BuiltinInfo None = {"", nullptr};
  229. constexpr BuiltinInfo NoOp = {"no_op", ValidateNoOpSignature};
  230. // Prints a single character.
  231. constexpr BuiltinInfo PrintChar = {
  232. "print.char", ValidateSignature<auto(AnySizedInt)->AnySizedInt>};
  233. // Prints an integer.
  234. constexpr BuiltinInfo PrintInt = {
  235. "print.int", ValidateSignature<auto(AnySizedInt)->NoReturn>};
  236. // Reads a single character from stdin.
  237. constexpr BuiltinInfo ReadChar = {"read.char",
  238. ValidateSignature<auto()->AnySizedInt>};
  239. // Returns the `Core.CharLiteral` type.
  240. constexpr BuiltinInfo CharLiteralMakeType = {"char_literal.make_type",
  241. ValidateSignature<auto()->Type>};
  242. // Returns the `Core.IntLiteral` type.
  243. constexpr BuiltinInfo IntLiteralMakeType = {"int_literal.make_type",
  244. ValidateSignature<auto()->Type>};
  245. // Returns the `Core.FloatLiteral` type.
  246. constexpr BuiltinInfo FloatLiteralMakeType = {"float_literal.make_type",
  247. ValidateSignature<auto()->Type>};
  248. // Returns the `iN` type.
  249. // TODO: Should we use a more specific type as the type of the bit width?
  250. constexpr BuiltinInfo IntMakeTypeSigned = {
  251. "int.make_type_signed", ValidateSignature<auto(AnyInt)->Type>};
  252. // Returns the `uN` type.
  253. constexpr BuiltinInfo IntMakeTypeUnsigned = {
  254. "int.make_type_unsigned", ValidateSignature<auto(AnyInt)->Type>};
  255. // Returns float types, such as `f64`. Currently only supports `f64`.
  256. constexpr BuiltinInfo FloatMakeType = {"float.make_type",
  257. ValidateSignature<auto(AnyInt)->Type>};
  258. // Returns the `bool` type.
  259. constexpr BuiltinInfo BoolMakeType = {"bool.make_type",
  260. ValidateSignature<auto()->Type>};
  261. // Converts between char types, with a diagnostic if the value doesn't fit.
  262. constexpr BuiltinInfo CharConvertChecked = {
  263. "char.convert_checked",
  264. ValidateSignature<auto(CharLiteral)->CharCompatible>};
  265. // Converts between integer types, truncating if necessary.
  266. constexpr BuiltinInfo IntConvert = {"int.convert",
  267. ValidateSignature<auto(AnyInt)->AnyInt>};
  268. // Converts between integer types, with a diagnostic if the value doesn't fit.
  269. constexpr BuiltinInfo IntConvertChecked = {
  270. "int.convert_checked", ValidateSignature<auto(AnyInt)->AnyInt>};
  271. // "int.snegate": integer negation.
  272. constexpr BuiltinInfo IntSNegate = {"int.snegate",
  273. ValidateSignature<auto(IntT)->IntT>};
  274. // "int.sadd": integer addition.
  275. constexpr BuiltinInfo IntSAdd = {"int.sadd",
  276. ValidateSignature<auto(IntT, IntT)->IntT>};
  277. // "int.ssub": integer subtraction.
  278. constexpr BuiltinInfo IntSSub = {"int.ssub",
  279. ValidateSignature<auto(IntT, IntT)->IntT>};
  280. // "int.smul": integer multiplication.
  281. constexpr BuiltinInfo IntSMul = {"int.smul",
  282. ValidateSignature<auto(IntT, IntT)->IntT>};
  283. // "int.sdiv": integer division.
  284. constexpr BuiltinInfo IntSDiv = {"int.sdiv",
  285. ValidateSignature<auto(IntT, IntT)->IntT>};
  286. // "int.smod": integer modulo.
  287. constexpr BuiltinInfo IntSMod = {"int.smod",
  288. ValidateSignature<auto(IntT, IntT)->IntT>};
  289. // "int.unegate": unsigned integer negation.
  290. constexpr BuiltinInfo IntUNegate = {
  291. "int.unegate", ValidateSignature<auto(SizedIntT)->SizedIntT>};
  292. // "int.uadd": unsigned integer addition.
  293. constexpr BuiltinInfo IntUAdd = {
  294. "int.uadd", ValidateSignature<auto(SizedIntT, SizedIntT)->SizedIntT>};
  295. // "int.usub": unsigned integer subtraction.
  296. constexpr BuiltinInfo IntUSub = {
  297. "int.usub", ValidateSignature<auto(SizedIntT, SizedIntT)->SizedIntT>};
  298. // "int.umul": unsigned integer multiplication.
  299. constexpr BuiltinInfo IntUMul = {
  300. "int.umul", ValidateSignature<auto(SizedIntT, SizedIntT)->SizedIntT>};
  301. // "int.udiv": unsigned integer division.
  302. constexpr BuiltinInfo IntUDiv = {
  303. "int.udiv", ValidateSignature<auto(SizedIntT, SizedIntT)->SizedIntT>};
  304. // "int.mod": integer modulo.
  305. constexpr BuiltinInfo IntUMod = {
  306. "int.umod", ValidateSignature<auto(SizedIntT, SizedIntT)->SizedIntT>};
  307. // "int.complement": integer bitwise complement.
  308. constexpr BuiltinInfo IntComplement = {"int.complement",
  309. ValidateSignature<auto(IntT)->IntT>};
  310. // "int.and": integer bitwise and.
  311. constexpr BuiltinInfo IntAnd = {"int.and",
  312. ValidateSignature<auto(IntT, IntT)->IntT>};
  313. // "int.or": integer bitwise or.
  314. constexpr BuiltinInfo IntOr = {"int.or",
  315. ValidateSignature<auto(IntT, IntT)->IntT>};
  316. // "int.xor": integer bitwise xor.
  317. constexpr BuiltinInfo IntXor = {"int.xor",
  318. ValidateSignature<auto(IntT, IntT)->IntT>};
  319. // "int.left_shift": integer left shift.
  320. constexpr BuiltinInfo IntLeftShift = {
  321. "int.left_shift", ValidateSignature<auto(IntT, IntU)->IntT>};
  322. // "int.right_shift": integer right shift.
  323. constexpr BuiltinInfo IntRightShift = {
  324. "int.right_shift", ValidateSignature<auto(IntT, IntU)->IntT>};
  325. // "int.sadd_assign": integer in-place addition.
  326. constexpr BuiltinInfo IntSAddAssign = {
  327. "int.sadd_assign",
  328. ValidateSignature<auto(PointerTo<SizedIntT>, SizedIntT)->NoReturn>};
  329. // "int.ssub_assign": integer in-place subtraction.
  330. constexpr BuiltinInfo IntSSubAssign = {
  331. "int.ssub_assign",
  332. ValidateSignature<auto(PointerTo<SizedIntT>, SizedIntT)->NoReturn>};
  333. // "int.smul_assign": integer in-place multiplication.
  334. constexpr BuiltinInfo IntSMulAssign = {
  335. "int.smul_assign",
  336. ValidateSignature<auto(PointerTo<SizedIntT>, SizedIntT)->NoReturn>};
  337. // "int.sdiv_assign": integer in-place division.
  338. constexpr BuiltinInfo IntSDivAssign = {
  339. "int.sdiv_assign",
  340. ValidateSignature<auto(PointerTo<SizedIntT>, SizedIntT)->NoReturn>};
  341. // "int.smod_assign": integer in-place modulo.
  342. constexpr BuiltinInfo IntSModAssign = {
  343. "int.smod_assign",
  344. ValidateSignature<auto(PointerTo<SizedIntT>, SizedIntT)->NoReturn>};
  345. // "int.uadd_assign": unsigned integer in-place addition.
  346. constexpr BuiltinInfo IntUAddAssign = {
  347. "int.uadd_assign",
  348. ValidateSignature<auto(PointerTo<SizedIntT>, SizedIntT)->NoReturn>};
  349. // "int.usub_assign": unsigned integer in-place subtraction.
  350. constexpr BuiltinInfo IntUSubAssign = {
  351. "int.usub_assign",
  352. ValidateSignature<auto(PointerTo<SizedIntT>, SizedIntT)->NoReturn>};
  353. // "int.umul_assign": unsigned integer in-place multiplication.
  354. constexpr BuiltinInfo IntUMulAssign = {
  355. "int.umul_assign",
  356. ValidateSignature<auto(PointerTo<SizedIntT>, SizedIntT)->NoReturn>};
  357. // "int.udiv_assign": unsigned integer in-place division.
  358. constexpr BuiltinInfo IntUDivAssign = {
  359. "int.udiv_assign",
  360. ValidateSignature<auto(PointerTo<SizedIntT>, SizedIntT)->NoReturn>};
  361. // "int.mod_assign": integer in-place modulo.
  362. constexpr BuiltinInfo IntUModAssign = {
  363. "int.umod_assign",
  364. ValidateSignature<auto(PointerTo<SizedIntT>, SizedIntT)->NoReturn>};
  365. // "int.and_assign": integer in-place bitwise and.
  366. constexpr BuiltinInfo IntAndAssign = {
  367. "int.and_assign",
  368. ValidateSignature<auto(PointerTo<SizedIntT>, SizedIntT)->NoReturn>};
  369. // "int.or_assign": integer in-place bitwise or.
  370. constexpr BuiltinInfo IntOrAssign = {
  371. "int.or_assign",
  372. ValidateSignature<auto(PointerTo<SizedIntT>, SizedIntT)->NoReturn>};
  373. // "int.xor_assign": integer in-place bitwise xor.
  374. constexpr BuiltinInfo IntXorAssign = {
  375. "int.xor_assign",
  376. ValidateSignature<auto(PointerTo<SizedIntT>, SizedIntT)->NoReturn>};
  377. // "int.left_shift_assign": integer in-place left shift.
  378. constexpr BuiltinInfo IntLeftShiftAssign = {
  379. "int.left_shift_assign",
  380. ValidateSignature<auto(PointerTo<SizedIntT>, SizedIntU)->NoReturn>};
  381. // "int.right_shift_assign": integer in-place right shift.
  382. constexpr BuiltinInfo IntRightShiftAssign = {
  383. "int.right_shift_assign",
  384. ValidateSignature<auto(PointerTo<SizedIntT>, SizedIntU)->NoReturn>};
  385. // "int.eq": integer equality comparison.
  386. constexpr BuiltinInfo IntEq = {"int.eq",
  387. ValidateSignature<auto(IntT, IntU)->Bool>};
  388. // "int.neq": integer non-equality comparison.
  389. constexpr BuiltinInfo IntNeq = {"int.neq",
  390. ValidateSignature<auto(IntT, IntU)->Bool>};
  391. // "int.less": integer less than comparison.
  392. constexpr BuiltinInfo IntLess = {"int.less",
  393. ValidateSignature<auto(IntT, IntU)->Bool>};
  394. // "int.less_eq": integer less than or equal comparison.
  395. constexpr BuiltinInfo IntLessEq = {"int.less_eq",
  396. ValidateSignature<auto(IntT, IntU)->Bool>};
  397. // "int.greater": integer greater than comparison.
  398. constexpr BuiltinInfo IntGreater = {"int.greater",
  399. ValidateSignature<auto(IntT, IntU)->Bool>};
  400. // "int.greater_eq": integer greater than or equal comparison.
  401. constexpr BuiltinInfo IntGreaterEq = {
  402. "int.greater_eq", ValidateSignature<auto(IntT, IntU)->Bool>};
  403. // "float.negate": float negation.
  404. constexpr BuiltinInfo FloatNegate = {"float.negate",
  405. ValidateSignature<auto(FloatT)->FloatT>};
  406. // "float.add": float addition.
  407. constexpr BuiltinInfo FloatAdd = {
  408. "float.add", ValidateSignature<auto(FloatT, FloatT)->FloatT>};
  409. // "float.sub": float subtraction.
  410. constexpr BuiltinInfo FloatSub = {
  411. "float.sub", ValidateSignature<auto(FloatT, FloatT)->FloatT>};
  412. // "float.mul": float multiplication.
  413. constexpr BuiltinInfo FloatMul = {
  414. "float.mul", ValidateSignature<auto(FloatT, FloatT)->FloatT>};
  415. // "float.div": float division.
  416. constexpr BuiltinInfo FloatDiv = {
  417. "float.div", ValidateSignature<auto(FloatT, FloatT)->FloatT>};
  418. // "float.add_assign": float in-place addition.
  419. constexpr BuiltinInfo FloatAddAssign = {
  420. "float.add_assign",
  421. ValidateSignature<auto(PointerTo<FloatT>, FloatT)->NoReturn>};
  422. // "float.sub_assign": float in-place subtraction.
  423. constexpr BuiltinInfo FloatSubAssign = {
  424. "float.sub_assign",
  425. ValidateSignature<auto(PointerTo<FloatT>, FloatT)->NoReturn>};
  426. // "float.mul_assign": float in-place multiplication.
  427. constexpr BuiltinInfo FloatMulAssign = {
  428. "float.mul_assign",
  429. ValidateSignature<auto(PointerTo<FloatT>, FloatT)->NoReturn>};
  430. // "float.div_assign": float in-place division.
  431. constexpr BuiltinInfo FloatDivAssign = {
  432. "float.div_assign",
  433. ValidateSignature<auto(PointerTo<FloatT>, FloatT)->NoReturn>};
  434. // Converts between floating-point types, with a diagnostic if the value doesn't
  435. // fit.
  436. constexpr BuiltinInfo FloatConvertChecked = {
  437. "float.convert_checked", ValidateSignature<auto(FloatT)->FloatU>};
  438. // "float.eq": float equality comparison.
  439. constexpr BuiltinInfo FloatEq = {"float.eq",
  440. ValidateSignature<auto(FloatT, FloatT)->Bool>};
  441. // "float.neq": float non-equality comparison.
  442. constexpr BuiltinInfo FloatNeq = {
  443. "float.neq", ValidateSignature<auto(FloatT, FloatT)->Bool>};
  444. // "float.less": float less than comparison.
  445. constexpr BuiltinInfo FloatLess = {
  446. "float.less", ValidateSignature<auto(FloatT, FloatT)->Bool>};
  447. // "float.less_eq": float less than or equal comparison.
  448. constexpr BuiltinInfo FloatLessEq = {
  449. "float.less_eq", ValidateSignature<auto(FloatT, FloatT)->Bool>};
  450. // "float.greater": float greater than comparison.
  451. constexpr BuiltinInfo FloatGreater = {
  452. "float.greater", ValidateSignature<auto(FloatT, FloatT)->Bool>};
  453. // "float.greater_eq": float greater than or equal comparison.
  454. constexpr BuiltinInfo FloatGreaterEq = {
  455. "float.greater_eq", ValidateSignature<auto(FloatT, FloatT)->Bool>};
  456. // "bool.eq": bool equality comparison.
  457. constexpr BuiltinInfo BoolEq = {"bool.eq",
  458. ValidateSignature<auto(Bool, Bool)->Bool>};
  459. // "bool.neq": bool non-equality comparison.
  460. constexpr BuiltinInfo BoolNeq = {"bool.neq",
  461. ValidateSignature<auto(Bool, Bool)->Bool>};
  462. // "type.and": facet type combination.
  463. constexpr BuiltinInfo TypeAnd = {"type.and",
  464. ValidateSignature<auto(Type, Type)->Type>};
  465. } // namespace BuiltinFunctionInfo
  466. CARBON_DEFINE_ENUM_CLASS_NAMES(BuiltinFunctionKind) = {
  467. #define CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(Name) \
  468. BuiltinFunctionInfo::Name.name,
  469. #include "toolchain/sem_ir/builtin_function_kind.def"
  470. };
  471. // Returns the builtin function kind with the given name, or None if the name
  472. // is unknown.
  473. auto BuiltinFunctionKind::ForBuiltinName(llvm::StringRef name)
  474. -> BuiltinFunctionKind {
  475. #define CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(Name) \
  476. if (name == BuiltinFunctionInfo::Name.name) { \
  477. return BuiltinFunctionKind::Name; \
  478. }
  479. #include "toolchain/sem_ir/builtin_function_kind.def"
  480. return BuiltinFunctionKind::None;
  481. }
  482. auto BuiltinFunctionKind::IsValidType(const File& sem_ir,
  483. llvm::ArrayRef<TypeId> arg_types,
  484. TypeId return_type) const -> bool {
  485. static constexpr ValidateFn* ValidateFns[] = {
  486. #define CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(Name) \
  487. BuiltinFunctionInfo::Name.validate,
  488. #include "toolchain/sem_ir/builtin_function_kind.def"
  489. };
  490. return ValidateFns[AsInt()](sem_ir, arg_types, return_type);
  491. }
  492. // Determines whether a builtin call involves an integer literal in its
  493. // arguments or return type. If so, for many builtins we want to treat the call
  494. // as being compile-time-only. This is because `Core.IntLiteral` has an empty
  495. // runtime representation, and a value of that type isn't necessarily a
  496. // compile-time constant, so an arbitrary runtime value of type
  497. // `Core.IntLiteral` may not have a value available for the builtin to use. For
  498. // example, given:
  499. //
  500. // var n: Core.IntLiteral() = 123;
  501. //
  502. // we would be unable to lower a runtime operation such as `(1 as i32) << n`
  503. // because the runtime representation of `n` doesn't track its value at all.
  504. //
  505. // For now, we treat all operations involving `Core.IntLiteral` as being
  506. // compile-time-only.
  507. //
  508. // TODO: We will need to accept things like `some_i32 << 5` eventually. We could
  509. // allow builtin calls at runtime if all the IntLiteral arguments have constant
  510. // values, or add logic to the prelude to promote the `IntLiteral` operand to a
  511. // different type in such cases.
  512. //
  513. // TODO: For now, we also treat builtins *returning* `Core.IntLiteral` as being
  514. // compile-time-only. This is mostly done for simplicity, but should probably be
  515. // revisited.
  516. static auto AnyIntLiteralTypes(const File& sem_ir,
  517. llvm::ArrayRef<InstId> arg_ids,
  518. TypeId return_type_id) -> bool {
  519. if (sem_ir.types().Is<IntLiteralType>(return_type_id)) {
  520. return true;
  521. }
  522. for (auto arg_id : arg_ids) {
  523. if (sem_ir.types().Is<IntLiteralType>(
  524. sem_ir.insts().Get(arg_id).type_id())) {
  525. return true;
  526. }
  527. }
  528. return false;
  529. }
  530. auto BuiltinFunctionKind::IsCompTimeOnly(const File& sem_ir,
  531. llvm::ArrayRef<InstId> arg_ids,
  532. TypeId return_type_id) const -> bool {
  533. switch (*this) {
  534. case CharConvertChecked:
  535. case FloatConvertChecked:
  536. case IntConvertChecked:
  537. // Checked conversions are compile-time only.
  538. return true;
  539. case IntConvert:
  540. case IntSNegate:
  541. case IntComplement:
  542. case IntSAdd:
  543. case IntSSub:
  544. case IntSMul:
  545. case IntSDiv:
  546. case IntSMod:
  547. case IntAnd:
  548. case IntOr:
  549. case IntXor:
  550. case IntLeftShift:
  551. case IntRightShift:
  552. case IntEq:
  553. case IntNeq:
  554. case IntLess:
  555. case IntLessEq:
  556. case IntGreater:
  557. case IntGreaterEq:
  558. // Integer operations are compile-time-only if they involve integer
  559. // literal types. See AnyIntLiteralTypes comment for explanation.
  560. return AnyIntLiteralTypes(sem_ir, arg_ids, return_type_id);
  561. case TypeAnd:
  562. return true;
  563. default:
  564. // TODO: Should the sized MakeType functions be compile-time only? We
  565. // can't produce diagnostics for bad sizes at runtime.
  566. return false;
  567. }
  568. }
  569. } // namespace Carbon::SemIR