Skip to content

Commit 7aaab70

Browse files
committed
More AI
1 parent d658744 commit 7aaab70

File tree

1 file changed

+44
-24
lines changed
  • crates/rustc_codegen_nvvm/src

1 file changed

+44
-24
lines changed

crates/rustc_codegen_nvvm/src/ty.rs

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -121,35 +121,55 @@ impl<'ll> CodegenCx<'ll, '_> {
121121
pub(crate) fn type_ptr_to_ext(&self, ty: &'ll Type, address_space: AddressSpace) -> &'ll Type {
122122
let result_ty = unsafe { llvm::LLVMPointerType(ty, address_space.0) };
123123

124-
// Specific workaround for LLVM 7 / NVPTX if LLVMPointerType(ptr, AS) -> ptr
125-
// This condition checks if we asked for a pointer to a pointer type,
126-
// but got back the same pointer type (not a pointer-to-pointer).
124+
// Workaround for LLVM 7 / NVPTX if LLVMPointerType(ptr, AS) -> ptr
125+
// Check if the input `ty` was already a pointer and if `LLVMPointerType`
126+
// returned the same type, indicating no new level of indirection was added.
127127
if result_ty == ty && unsafe { llvm::LLVMRustGetTypeKind(ty) == llvm::TypeKind::Pointer } {
128-
// Check if 'ty' is specifically the generic data pointer (like i8* in default AS for data).
129-
// For NVPTX, AddressSpace::DATA is often 0 (Generic) or 1 (Global).
130-
// Let's assume AddressSpace::DATA (0 for generic ptr) is the one involved.
131-
let generic_data_ptr_type = self.type_ptr_ext(AddressSpace::DATA); // Gets i8* in AddressSpace::DATA
132-
133-
if ty == generic_data_ptr_type {
134-
// We attempted to create a pointer to the generic data pointer type
135-
// (e.g., `(i8 addrspace(N))*`) but `LLVMPointerType` returned the
136-
// generic data pointer type itself (`i8 addrspace(N)*`).
137-
let i8_llty = self.type_i8();
138-
let i8_ptr_llty = unsafe { llvm::LLVMPointerType(i8_llty, address_space.0) }; // Should be `i8*` (or `ptr`)
139-
let i8_ptr_ptr_llty =
140-
unsafe { llvm::LLVMPointerType(i8_ptr_llty, address_space.0) }; // Should be `i8**` (or `ptr*`)
141-
142-
if ty == i8_ptr_llty && result_ty == i8_ptr_llty {
143-
// If input was i8* (ptr) and output was i8* (ptr)
144-
// This means LLVMPointerType(i8*, AS) -> i8*
145-
// We need i8**.
128+
// This is the "quirk" situation. We need to specifically identify if `ty`
129+
// is the generic data pointer that's causing the issue. For LLVM 7 on
130+
// NVPTX, this is typically equivalent to an i8 pointer in a default data
131+
// address space (e.g., AS 0 or 1).
132+
133+
// Define what the generic `ptr` type looks like when created from `i8`.
134+
// Assuming AddressSpace::DATA is the relevant one for such generic pointers.
135+
let i8_llty = self.type_i8();
136+
let canonical_generic_ptr_llty_in_data_as =
137+
unsafe { llvm::LLVMPointerType(i8_llty, AddressSpace::DATA.0) };
138+
139+
// Now, check if our input `ty` IS this canonical generic data pointer
140+
// AND if we were trying to create a pointer to it in the *same* AddressSpace::DATA.
141+
// (If address_space was different, LLVM should naturally create a new pointer type).
142+
if ty == canonical_generic_ptr_llty_in_data_as && address_space == AddressSpace::DATA {
143+
// We tried to make `(i8* AS_DATA)* AS_DATA` but got `i8* AS_DATA` back.
144+
// This is the problematic case. We need to force what should be `(i8*
145+
// AS_DATA)* AS_DATA`.
146+
let ptr_to_canonical_generic_ptr_llty = unsafe {
147+
llvm::LLVMPointerType(
148+
canonical_generic_ptr_llty_in_data_as,
149+
AddressSpace::DATA.0,
150+
)
151+
};
152+
153+
// If ptr_to_canonical_generic_ptr_llty is *still* the same as canonical_generic_ptr_llty,
154+
// then LLVM 7 truly cannot make a distinct `ptr*` from `ptr` using LLVMPointerType
155+
if ptr_to_canonical_generic_ptr_llty != canonical_generic_ptr_llty {
146156
tracing::debug!(
147-
"Applying LLVMPointerType quirk workaround for ptr-to-ptr. Requested ptr to {:?}, got same. Forcing to pointer to (pointer to i8). Old result: {:?}, New result: {:?}",
157+
"type_ptr_to_ext: Quirk detected for generic ptr. Input ty: {:?}, Requested AS: {:?}. Initial result: {:?}. Forced to: {:?}",
148158
ty,
159+
address_space.0,
149160
result_ty,
150-
i8_ptr_ptr_llty
161+
ptr_to_canonical_generic_ptr_llty
162+
);
163+
return ptr_to_canonical_generic_ptr_llty;
164+
} else {
165+
// This case means LLVMPointerType(i8*_AS_DATA, AS_DATA) -> i8*_AS_DATA
166+
// AND LLVMPointerType( (i8*_AS_DATA), AS_DATA) -> i8*_AS_DATA (still!)
167+
tracing::warn!(
168+
"type_ptr_to_ext: LLVM 7 / NVPTX ptr quirk: LLVMPointerType cannot create distinct ptr-to-ptr for generic ptr type {:?}. This will likely lead to errors.",
169+
ty
151170
);
152-
return i8_ptr_ptr_llty;
171+
// Return original result_ty, as we can't improve it.
172+
return result_ty;
153173
}
154174
}
155175
}

0 commit comments

Comments
 (0)