<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Oct 13, 2017, at 5:33 PM, Siddharth Bhat <<a href="mailto:siddu.druid@gmail.com" class="">siddu.druid@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><p dir="ltr" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">I see. So, compatibility of two values depends on address spaces as well? I didn't know that, thank you.</p><p dir="ltr" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">In that case, is there some uniform way to say "give me a new instruction with this (possibly incompatible) value"?<span class="Apple-converted-space"> </span></p><div class=""><br class=""></div></div></blockquote>Usually you have to add casts if the value types don’t match.</div><div>Look in InstCombine and such for examples.<br class=""><br class=""><blockquote type="cite" class=""><div class=""><p dir="ltr" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">For context, I'm taking some code that was generated in a different address space context, and I'm copying it to a different context (with a different address space).<br class=""></p></div></blockquote><div><br class=""></div>You’ll need a addrspace cast to do that.</div><div><br class=""><blockquote type="cite" class=""><div class=""><p dir="ltr" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">Thanks,<br class="">Siddharth.</p><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><div class="gmail_quote" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><div dir="ltr" class="">On Sat 14 Oct, 2017, 02:30 Quentin Colombet, <<a href="mailto:qcolombet@apple.com" class="">qcolombet@apple.com</a>> wrote:<br class=""></div><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(204, 204, 204); padding-left: 1ex;"><div style="word-wrap: break-word; line-break: after-white-space;" class=""><div class="">Hi <span style="font-family: sans-serif; white-space: pre-wrap;" class="">Siddharth,</span></div><div class=""><span style="font-family: sans-serif; white-space: pre-wrap;" class=""><br class=""></span></div><div class=""><font face="sans-serif" class=""><span style="white-space: pre-wrap;" class="">I haven’t read through your code or example, so my reply may be off but I would say this is expected.</span></font></div><div class=""><font face="sans-serif" class=""><span style="white-space: pre-wrap;" class="">When you call replaceUsesOfWith, you have to make sure the values are “compatible” before doing this.</span></font></div><div class=""><font face="sans-serif" class=""><span style="white-space: pre-wrap;" class="">If the address space of both values are different and that’s not okay, then you will create wrong code.</span></font></div><div class=""><font face="sans-serif" class=""><span style="white-space: pre-wrap;" class=""><br class=""></span></font></div><div class=""><font face="sans-serif" class=""><span style="white-space: pre-wrap;" class="">Cheers,</span></font></div><div class=""><font face="sans-serif" class=""><span style="white-space: pre-wrap;" class="">-Quentin</span></font></div><br class=""><div class=""><blockquote type="cite" class=""></blockquote></div></div><div style="word-wrap: break-word; line-break: after-white-space;" class=""><div class=""><blockquote type="cite" class=""><div class="">On Oct 13, 2017, at 5:17 PM, Siddharth Bhat via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank" class="">llvm-dev@lists.llvm.org</a>> wrote:</div><br class="m_-3210939756874267889Apple-interchange-newline"></blockquote></div></div><div style="word-wrap: break-word; line-break: after-white-space;" class=""><div class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class="">Hello,<div class=""><br class=""></div><div class="">Calling `replaceUsesOfWith` with a value in a different addrspace does not keep the addrspace of a GEP consistent. <span style="white-space: pre-wrap;" class="">Is this known? Is this a bug or expected behaviour?</span></div><div class=""><br class=""></div><div class=""><a href="https://gist.github.com/bollu/152ba5e1c20c03c7fc6d8c7b23ba828f" target="_blank" class="">Minimal counterexample link</a></div><div class=""><br class=""></div><div class="">Reproduced here:</div><div class=""><br class=""></div><div class=""><pre style="word-wrap: break-word; white-space: pre-wrap;" class="">#include <iostream>
#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Bitcode/BitcodeWriter.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Support/FileSystem.h"
using namespace llvm;
using namespace std;

static const bool NON_VAR_ARG = false;

static AssertingVH<Function> getOrCreateFunction(Module &m, FunctionType *FTy,
                                                 std::string name) {
    Function *F = m.getFunction(name);
    if (F) return F;

    return Function::Create(FTy, GlobalValue::ExternalLinkage, name, &m);
};
static const bool SHOW_ERROR = false;

int main() {
    static LLVMContext ctx;
    static IRBuilder<> Builder(ctx);

    Module *m = new Module("Module", ctx);
    Function *F = getOrCreateFunction(
        *m,
        FunctionType::get(Builder.getInt64Ty(),
                          {PointerType::get(Builder.getInt32Ty(), 42),
                          PointerType::get(Builder.getInt32Ty(), 1)},
                          NON_VAR_ARG),
        "f");
    auto It = F->arg_begin();
    Value *Arg = &*It;
    It++;
    Value *Arg2 = &*It;

    BasicBlock *Entry = BasicBlock::Create(ctx, "entry", F);
    Builder.SetInsertPoint(Entry);

    Instruction *Slot = nullptr;
<b class="">    if (SHOW_ERROR) {
        Slot = cast<Instruction>(Builder.CreateGEP(Arg, {Builder.getInt64(1)}, "slot"));
        errs() << "Slot(original): " << *Slot << "\n";
        Slot->replaceUsesOfWith(Arg, Arg2);
        errs() << "Slot(replaced): " << *Slot << "\n";
    }
    else {
        Slot = cast<Instruction>(Builder.CreateGEP(Arg2, {Builder.getInt64(1)}, "slot"));
    }
</b>    Value *TypedSlot = Builder.CreateBitCast(Slot, PointerType::get(Builder.getInt64Ty(), 1), "slot_typed");
    Value *Load = Builder.CreateLoad(TypedSlot, "Val");
    Builder.CreateRet(Load);

    if (verifyModule(*m) == 1) {
        errs() << "module has an error: ";
        verifyModule(*m, &errs());
        report_fatal_error("buggy module.");
    }
    outs() << *m << "\n";
    // llvm::WriteBitcodeToFile(m, outs());

    return 1;
};</pre><pre style="word-wrap: break-word; white-space: pre-wrap;" class=""><br class=""></pre><pre style="word-wrap: break-word; white-space: pre-wrap;" class=""><br class=""></pre><pre style="word-wrap: break-word; white-space: pre-wrap;" class="">Output:</pre><pre style="word-wrap: break-word; white-space: pre-wrap;" class="">### `SHOW_ERROR = 0`
```ll
; ModuleID = 'Module'
source_filename = "Module"

define i64 @f(i32 addrspace(42)*, i32 addrspace(1)*) {
entry:
  %slot = getelementptr i32, i32 addrspace(1)* %1, i64 1
  %slot_typed = bitcast i32 addrspace(1)* %slot to i64 addrspace(1)*
  %Val = load i64, i64 addrspace(1)* %slot_typed
  ret i64 %Val
}
```

### `SHOW_ERROR = 1`
```
Slot(original):   %slot = getelementptr i32, i32 addrspace(42)* %0, i64 1
Slot(replaced):   %slot = getelementptr i32, i32 addrspace(1)* %1, i64 1
<b class="">Assertion failed: (castIsValid(op, S, Ty) && "Invalid cast!"), function Create, file /Users/bollu/work/LLVM-all/polly/llvm/lib/IR/Instructions.cpp, line 2592.
</b>[5]    56333 abort      ./prog
```</pre><pre style="word-wrap: break-word; white-space: pre-wrap;" class=""><br class=""></pre><pre style="word-wrap: break-word; white-space: pre-wrap;" class=""><font face="sans-serif" class="">Thanks,</font></pre><pre style="word-wrap: break-word; white-space: pre-wrap;" class=""><font face="sans-serif" class="">~Siddharth.</font></pre></div></div><div dir="ltr" class="">--<span class="Apple-converted-space"> </span><br class=""></div><div class="m_-3210939756874267889gmail_signature" data-smartmail="gmail_signature"><div dir="ltr" class="">Sending this from my phone, please excuse any typos!</div></div></div></blockquote></div></div><div style="word-wrap: break-word; line-break: after-white-space;" class=""><div class=""><blockquote type="cite" class=""><div class="">_______________________________________________<br class="">LLVM Developers mailing list<br class=""><a href="mailto:llvm-dev@lists.llvm.org" target="_blank" class="">llvm-dev@lists.llvm.org</a><br class=""><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" target="_blank" class="">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br class=""></div></blockquote></div><br class=""></div></blockquote></div><div dir="ltr" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">--<span class="Apple-converted-space"> </span><br class=""></div><div class="gmail_signature" data-smartmail="gmail_signature" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><div dir="ltr" class="">Sending this from my phone, please excuse any typos!</div></div></div></blockquote></div><br class=""></body></html>