[clang] [clang] Lower _BitInt(129+) to a different type in LLVM IR (PR #91364)

Mariya Podchishchaeva via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 5 04:53:15 PDT 2024


================
@@ -118,6 +124,37 @@ llvm::Type *CodeGenTypes::ConvertTypeForMem(QualType T, bool ForBitField) {
   return R;
 }
 
+bool CodeGenTypes::LLVMTypeLayoutMatchesAST(QualType ASTTy,
+                                            llvm::Type *LLVMTy) {
+  CharUnits ASTSize = Context.getTypeSizeInChars(ASTTy);
+  CharUnits LLVMSize =
+      CharUnits::fromQuantity(getDataLayout().getTypeAllocSize(LLVMTy));
+  return ASTSize == LLVMSize;
+}
+
+llvm::Type *CodeGenTypes::convertTypeForLoadStore(QualType T,
+                                                  llvm::Type *LLVMTy) {
+  if (!LLVMTy)
+    LLVMTy = ConvertType(T);
+
+  if (!T->isBitIntType() && LLVMTy->isIntegerTy(1))
+    return llvm::IntegerType::get(getLLVMContext(),
+                                  (unsigned)Context.getTypeSize(T));
+
+  if (T->isBitIntType()) {
+    llvm::Type *R = ConvertType(T);
+    if (!LLVMTypeLayoutMatchesAST(T, R))
+      return llvm::Type::getIntNTy(
+          getLLVMContext(), Context.getTypeSizeInChars(T).getQuantity() * 8);
----------------
Fznamznon wrote:

We intend to support all types. In general, _BitInt(N) is lowered to iN type in LLVM IR. However it is not possible for certain sizes and some targets now due to different alignment defined for iN and _BitInt(N). This now happens for _BitInt(>=129) for x86_64 targets. `LLVMTypeLayoutMatchesAST` checks for this mismatch. If there is mismatch, say NBytes = sizeof(_BitInt(N)), we use [i8 x NBytes] as in-memory type. It is better to load and store value using iNBytes type instead of an array type since it is better for optimization and guarantees that all bits of the loaded/stores value is valid. For more info, please refer to https://github.com/llvm/llvm-project/pull/91364#issuecomment-2099351151 as well.

https://github.com/llvm/llvm-project/pull/91364


More information about the cfe-commits mailing list