builtin_function_kind.cpp 27 KB

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