Skip to content

Commit be3ced1

Browse files
committed
AI workaround
1 parent feaed8c commit be3ced1

File tree

1 file changed

+33
-1
lines changed
  • crates/rustc_codegen_nvvm/src

1 file changed

+33
-1
lines changed

crates/rustc_codegen_nvvm/src/ty.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,39 @@ impl<'ll> CodegenCx<'ll, '_> {
120120
}
121121

122122
pub(crate) fn type_ptr_to_ext(&self, ty: &'ll Type, address_space: AddressSpace) -> &'ll Type {
123-
unsafe { llvm::LLVMPointerType(ty, address_space.0) }
123+
let result_ty = unsafe { llvm::LLVMPointerType(ty, address_space.0) };
124+
125+
// Specific workaround for LLVM 7 / NVPTX if LLVMPointerType(ptr, AS) -> ptr
126+
// This condition checks if we asked for a pointer to a pointer type,
127+
// but got back the same pointer type (not a pointer-to-pointer).
128+
if result_ty == ty && unsafe { llvm::LLVMGetTypeKind(ty) == llvm::TypeKind::Pointer } {
129+
// Check if 'ty' is specifically the generic data pointer (like i8* in default AS for data).
130+
// For NVPTX, AddressSpace::DATA is often 0 (Generic) or 1 (Global).
131+
// Let's assume AddressSpace::DATA (0 for generic ptr) is the one involved.
132+
let generic_data_ptr_type = self.type_ptr_ext(AddressSpace::DATA); // Gets i8* in AddressSpace::DATA
133+
134+
if ty == generic_data_ptr_type {
135+
// We attempted to create a pointer to the generic data pointer type
136+
// (e.g., `(i8 addrspace(N))*`) but `LLVMPointerType` returned the
137+
// generic data pointer type itself (`i8 addrspace(N)*`).
138+
let i8_llty = self.type_i8();
139+
let i8_ptr_llty = unsafe { llvm::LLVMPointerType(i8_llty, address_space.0) }; // Should be `i8*` (or `ptr`)
140+
let i8_ptr_ptr_llty =
141+
unsafe { llvm::LLVMPointerType(i8_ptr_llty, address_space.0) }; // Should be `i8**` (or `ptr*`)
142+
143+
if ty == i8_ptr_llty && result_ty == i8_ptr_llty {
144+
// If input was i8* (ptr) and output was i8* (ptr)
145+
// This means LLVMPointerType(i8*, AS) -> i8*
146+
// We need i8**.
147+
debug!(
148+
"Applying LLVMPointerType quirk workaround for ptr-to-ptr. Requested ptr to {:?}, got same. Forcing to pointer to (pointer to i8). Old result: {:?}, New result: {:?}",
149+
ty, result_ty, i8_ptr_ptr_llty
150+
);
151+
return i8_ptr_ptr_llty;
152+
}
153+
}
154+
}
155+
result_ty
124156
}
125157

126158
pub(crate) fn func_params_types(&self, ty: &'ll Type) -> Vec<&'ll Type> {

0 commit comments

Comments
 (0)