builtin_function_kind.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  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<FloatLiteralType::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. type_id = sem_ir.types().GetAdaptedType(type_id);
  146. }
  147. return false;
  148. }
  149. } // namespace
  150. // Validates that this builtin has a signature matching the specified signature.
  151. //
  152. // `SignatureFnType` is a C++ function type that describes the signature that is
  153. // expected for this builtin. For example, `auto (AnyInt, AnyInt) -> AnyInt`
  154. // specifies that the builtin takes values of two integer types and returns a
  155. // value of a third integer type. Types used within the signature should provide
  156. // a `Check` function that validates that the Carbon type is expected:
  157. //
  158. // auto Check(const File&, ValidateState&, TypeId) -> bool;
  159. //
  160. // To constrain that the same type is used in multiple places in the signature,
  161. // `TypeParam<I, T>` can be used. For example:
  162. //
  163. // auto (TypeParam<0, AnyInt>, AnyInt) -> TypeParam<0, AnyInt>
  164. //
  165. // describes a builtin that takes two integers, and whose return type matches
  166. // its first parameter type. For convenience, typedefs for `TypeParam<I, T>`
  167. // are used in the descriptions of the builtins.
  168. template <typename SignatureFnType>
  169. static auto ValidateSignature(const File& sem_ir,
  170. llvm::ArrayRef<TypeId> arg_types,
  171. TypeId return_type) -> bool {
  172. using SignatureTraits = llvm::function_traits<SignatureFnType*>;
  173. ValidateState state;
  174. // Must have expected number of arguments.
  175. if (arg_types.size() != SignatureTraits::num_args) {
  176. return false;
  177. }
  178. // Argument types must match.
  179. if (![&]<size_t... Indexes>(std::index_sequence<Indexes...>) {
  180. return ((Check<typename SignatureTraits::template arg_t<Indexes>>(
  181. sem_ir, state, arg_types[Indexes])) &&
  182. ...);
  183. }(std::make_index_sequence<SignatureTraits::num_args>())) {
  184. return false;
  185. }
  186. // Result type must match.
  187. if (!Check<typename SignatureTraits::result_t>(sem_ir, state, return_type)) {
  188. return false;
  189. }
  190. return true;
  191. }
  192. // Validates the signature for NoOp. This ignores all arguments, only validating
  193. // that the return type is compatible.
  194. static auto ValidateNoOpSignature(const File& sem_ir,
  195. llvm::ArrayRef<TypeId> /*arg_types*/,
  196. TypeId return_type) -> bool {
  197. ValidateState state;
  198. return Check<NoReturn>(sem_ir, state, return_type);
  199. }
  200. // Descriptions of builtin functions follow. For each builtin, a corresponding
  201. // `BuiltinInfo` constant is declared describing properties of that builtin.
  202. namespace BuiltinFunctionInfo {
  203. // Convenience name used in the builtin type signatures below for a first
  204. // generic type parameter that is constrained to be an integer type.
  205. using IntT = TypeParam<0, AnyInt>;
  206. // Convenience name used in the builtin type signatures below for a second
  207. // generic type parameter that is constrained to be an integer type.
  208. using IntU = TypeParam<1, AnyInt>;
  209. // Convenience name used in the builtin type signatures below for a first
  210. // generic type parameter that is constrained to be a sized integer type.
  211. using SizedIntT = TypeParam<0, AnySizedInt>;
  212. // Convenience name used in the builtin type signatures below for a second
  213. // generic type parameter that is constrained to be a sized integer type.
  214. using SizedIntU = TypeParam<1, AnySizedInt>;
  215. // Convenience name used in the builtin type signatures below for a first
  216. // generic type parameter that is constrained to be an float type.
  217. using FloatT = TypeParam<0, AnyFloat>;
  218. // Convenience name used in the builtin type signatures below for a second
  219. // generic type parameter that is constrained to be an float type.
  220. using FloatU = TypeParam<1, AnyFloat>;
  221. // Convenience name used in the builtin type signatures below for a first
  222. // generic type parameter that is constrained to be a sized float type.
  223. using SizedFloatT = TypeParam<0, AnySizedFloat>;
  224. // Not a builtin function.
  225. constexpr BuiltinInfo None = {"", nullptr};
  226. constexpr BuiltinInfo NoOp = {"no_op", ValidateNoOpSignature};
  227. // Prints a single character.
  228. constexpr BuiltinInfo PrintChar = {
  229. "print.char", ValidateSignature<auto(AnySizedInt)->AnySizedInt>};
  230. // Prints an integer.
  231. constexpr BuiltinInfo PrintInt = {
  232. "print.int", ValidateSignature<auto(AnySizedInt)->NoReturn>};
  233. // Reads a single character from stdin.
  234. constexpr BuiltinInfo ReadChar = {"read.char",
  235. ValidateSignature<auto()->AnySizedInt>};
  236. // Returns the `Core.CharLiteral` type.
  237. constexpr BuiltinInfo CharLiteralMakeType = {"char_literal.make_type",
  238. ValidateSignature<auto()->Type>};
  239. // Returns the `Core.IntLiteral` type.
  240. constexpr BuiltinInfo IntLiteralMakeType = {"int_literal.make_type",
  241. ValidateSignature<auto()->Type>};
  242. // Returns the `Core.FloatLiteral` type.
  243. constexpr BuiltinInfo FloatLiteralMakeType = {"float_literal.make_type",
  244. ValidateSignature<auto()->Type>};
  245. // Returns the `iN` type.
  246. // TODO: Should we use a more specific type as the type of the bit width?
  247. constexpr BuiltinInfo IntMakeTypeSigned = {
  248. "int.make_type_signed", ValidateSignature<auto(AnyInt)->Type>};
  249. // Returns the `uN` type.
  250. constexpr BuiltinInfo IntMakeTypeUnsigned = {
  251. "int.make_type_unsigned", ValidateSignature<auto(AnyInt)->Type>};
  252. // Returns float types, such as `f64`. Currently only supports `f64`.
  253. constexpr BuiltinInfo FloatMakeType = {"float.make_type",
  254. ValidateSignature<auto(AnyInt)->Type>};
  255. // Returns the `bool` type.
  256. constexpr BuiltinInfo BoolMakeType = {"bool.make_type",
  257. ValidateSignature<auto()->Type>};
  258. // Returns the `MaybeUnformed(T)` type.
  259. constexpr BuiltinInfo MaybeUnformedMakeType = {
  260. "maybe_unformed.make_type", ValidateSignature<auto(Type)->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 = {
  405. "float.negate", ValidateSignature<auto(SizedFloatT)->SizedFloatT>};
  406. // "float.add": float addition.
  407. constexpr BuiltinInfo FloatAdd = {
  408. "float.add",
  409. ValidateSignature<auto(SizedFloatT, SizedFloatT)->SizedFloatT>};
  410. // "float.sub": float subtraction.
  411. constexpr BuiltinInfo FloatSub = {
  412. "float.sub",
  413. ValidateSignature<auto(SizedFloatT, SizedFloatT)->SizedFloatT>};
  414. // "float.mul": float multiplication.
  415. constexpr BuiltinInfo FloatMul = {
  416. "float.mul",
  417. ValidateSignature<auto(SizedFloatT, SizedFloatT)->SizedFloatT>};
  418. // "float.div": float division.
  419. constexpr BuiltinInfo FloatDiv = {
  420. "float.div",
  421. ValidateSignature<auto(SizedFloatT, SizedFloatT)->SizedFloatT>};
  422. // "float.add_assign": float in-place addition.
  423. constexpr BuiltinInfo FloatAddAssign = {
  424. "float.add_assign",
  425. ValidateSignature<auto(PointerTo<SizedFloatT>, SizedFloatT)->NoReturn>};
  426. // "float.sub_assign": float in-place subtraction.
  427. constexpr BuiltinInfo FloatSubAssign = {
  428. "float.sub_assign",
  429. ValidateSignature<auto(PointerTo<SizedFloatT>, SizedFloatT)->NoReturn>};
  430. // "float.mul_assign": float in-place multiplication.
  431. constexpr BuiltinInfo FloatMulAssign = {
  432. "float.mul_assign",
  433. ValidateSignature<auto(PointerTo<SizedFloatT>, SizedFloatT)->NoReturn>};
  434. // "float.div_assign": float in-place division.
  435. constexpr BuiltinInfo FloatDivAssign = {
  436. "float.div_assign",
  437. ValidateSignature<auto(PointerTo<SizedFloatT>, SizedFloatT)->NoReturn>};
  438. // Converts between floating-point types, with a diagnostic if the value doesn't
  439. // fit.
  440. constexpr BuiltinInfo FloatConvertChecked = {
  441. "float.convert_checked", ValidateSignature<auto(FloatT)->FloatU>};
  442. // "float.eq": float equality comparison.
  443. constexpr BuiltinInfo FloatEq = {
  444. "float.eq", ValidateSignature<auto(SizedFloatT, SizedFloatT)->Bool>};
  445. // "float.neq": float non-equality comparison.
  446. constexpr BuiltinInfo FloatNeq = {
  447. "float.neq", ValidateSignature<auto(SizedFloatT, SizedFloatT)->Bool>};
  448. // "float.less": float less than comparison.
  449. constexpr BuiltinInfo FloatLess = {
  450. "float.less", ValidateSignature<auto(SizedFloatT, SizedFloatT)->Bool>};
  451. // "float.less_eq": float less than or equal comparison.
  452. constexpr BuiltinInfo FloatLessEq = {
  453. "float.less_eq", ValidateSignature<auto(SizedFloatT, SizedFloatT)->Bool>};
  454. // "float.greater": float greater than comparison.
  455. constexpr BuiltinInfo FloatGreater = {
  456. "float.greater", ValidateSignature<auto(SizedFloatT, SizedFloatT)->Bool>};
  457. // "float.greater_eq": float greater than or equal comparison.
  458. constexpr BuiltinInfo FloatGreaterEq = {
  459. "float.greater_eq",
  460. ValidateSignature<auto(SizedFloatT, SizedFloatT)->Bool>};
  461. // "bool.eq": bool equality comparison.
  462. constexpr BuiltinInfo BoolEq = {"bool.eq",
  463. ValidateSignature<auto(Bool, Bool)->Bool>};
  464. // "bool.neq": bool non-equality comparison.
  465. constexpr BuiltinInfo BoolNeq = {"bool.neq",
  466. ValidateSignature<auto(Bool, Bool)->Bool>};
  467. // "type.and": facet type combination.
  468. constexpr BuiltinInfo TypeAnd = {"type.and",
  469. ValidateSignature<auto(Type, Type)->Type>};
  470. } // namespace BuiltinFunctionInfo
  471. CARBON_DEFINE_ENUM_CLASS_NAMES(BuiltinFunctionKind) = {
  472. #define CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(Name) \
  473. BuiltinFunctionInfo::Name.name,
  474. #include "toolchain/sem_ir/builtin_function_kind.def"
  475. };
  476. // Returns the builtin function kind with the given name, or None if the name
  477. // is unknown.
  478. auto BuiltinFunctionKind::ForBuiltinName(llvm::StringRef name)
  479. -> BuiltinFunctionKind {
  480. #define CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(Name) \
  481. if (name == BuiltinFunctionInfo::Name.name) { \
  482. return BuiltinFunctionKind::Name; \
  483. }
  484. #include "toolchain/sem_ir/builtin_function_kind.def"
  485. return BuiltinFunctionKind::None;
  486. }
  487. auto BuiltinFunctionKind::IsValidType(const File& sem_ir,
  488. llvm::ArrayRef<TypeId> arg_types,
  489. TypeId return_type) const -> bool {
  490. static constexpr ValidateFn* ValidateFns[] = {
  491. #define CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(Name) \
  492. BuiltinFunctionInfo::Name.validate,
  493. #include "toolchain/sem_ir/builtin_function_kind.def"
  494. };
  495. return ValidateFns[AsInt()](sem_ir, arg_types, return_type);
  496. }
  497. static auto IsLiteralType(const File& sem_ir, TypeId type_id) -> bool {
  498. // Unwrap adapters.
  499. type_id = sem_ir.types().GetTransitiveAdaptedType(type_id);
  500. auto type_inst_id = sem_ir.types().GetAsInst(type_id);
  501. return type_inst_id.Is<IntLiteralType>() ||
  502. type_inst_id.Is<FloatLiteralType>();
  503. }
  504. // Determines whether a builtin call involves an integer or floating-point
  505. // literal in its arguments or return type. If so, for many builtins we want to
  506. // treat the call as being compile-time-only. This is because `Core.IntLiteral`
  507. // and `Core.FloatLiteral` have an empty runtime representation, and a value of
  508. // such a type isn't necessarily a compile-time constant, so an arbitrary
  509. // runtime value of such a type may not have a value available for the builtin
  510. // to use. For example, given:
  511. //
  512. // var n: Core.IntLiteral() = 123;
  513. //
  514. // we would be unable to lower a runtime operation such as `(1 as i32) << n`
  515. // because the runtime representation of `n` doesn't track its value at all.
  516. //
  517. // For now, we treat all operations involving `Core.IntLiteral` or
  518. // `Core.FloatLiteral` as being compile-time-only.
  519. //
  520. // TODO: We will need to accept things like `some_i32 << 5` eventually. We could
  521. // allow builtin calls at runtime if all the IntLiteral arguments have constant
  522. // values, or add logic to the prelude to promote the `IntLiteral` operand to a
  523. // different type in such cases.
  524. //
  525. // TODO: For now, we also treat builtins *returning* `Core.IntLiteral` or
  526. // `Core.FloatLiteral` as being compile-time-only. This is mostly done for
  527. // simplicity, but should probably be revisited.
  528. static auto AnyLiteralTypes(const File& sem_ir, llvm::ArrayRef<InstId> arg_ids,
  529. TypeId return_type_id) -> bool {
  530. if (IsLiteralType(sem_ir, return_type_id)) {
  531. return true;
  532. }
  533. for (auto arg_id : arg_ids) {
  534. if (IsLiteralType(sem_ir, sem_ir.insts().Get(arg_id).type_id())) {
  535. return true;
  536. }
  537. }
  538. return false;
  539. }
  540. auto BuiltinFunctionKind::IsCompTimeOnly(const File& sem_ir,
  541. llvm::ArrayRef<InstId> arg_ids,
  542. TypeId return_type_id) const -> bool {
  543. switch (*this) {
  544. case CharConvertChecked:
  545. case FloatConvertChecked:
  546. case IntConvertChecked:
  547. // Checked conversions are compile-time only.
  548. return true;
  549. case IntConvert:
  550. case IntSNegate:
  551. case IntComplement:
  552. case IntSAdd:
  553. case IntSSub:
  554. case IntSMul:
  555. case IntSDiv:
  556. case IntSMod:
  557. case IntAnd:
  558. case IntOr:
  559. case IntXor:
  560. case IntLeftShift:
  561. case IntRightShift:
  562. case IntEq:
  563. case IntNeq:
  564. case IntLess:
  565. case IntLessEq:
  566. case IntGreater:
  567. case IntGreaterEq:
  568. // Integer operations are compile-time-only if they involve literal types.
  569. // See AnyLiteralTypes comment for explanation.
  570. return AnyLiteralTypes(sem_ir, arg_ids, return_type_id);
  571. case TypeAnd:
  572. return true;
  573. default:
  574. // TODO: Should the sized MakeType functions be compile-time only? We
  575. // can't produce diagnostics for bad sizes at runtime.
  576. return false;
  577. }
  578. }
  579. } // namespace Carbon::SemIR